MySQL:如何对现有记录集执行查询?

发布于 2024-10-24 21:00:51 字数 279 浏览 1 评论 0原文

我想对现有记录集执行 SELECT 查询,例如:

query1 = 'Select * from products where onSale = 1'

然后其中

query2 = 'Select * from recordset1 where ....'

recordset1query1 返回的行数。 有什么建议吗?谢谢!

I'd like to perform a SELECT query on an existing recordset, for example:

query1 = 'Select * from products where onSale = 1'

and then

query2 = 'Select * from recordset1 where ....'

where recordset1 is the number of rows that query1 returned.
Any suggestions? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

檐上三寸雪 2024-10-31 21:00:51

使用子查询:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

Use a subquery:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
小嗷兮 2024-10-31 21:00:51

如果这只是在 MySQL 中那么你可以执行

SELECT *
FROM (SELECT * FROM products WHERE onsale =1) AS OnSaleProducts

要使用您的示例,

请从以下位置选择不同的 colorid
(SELECT * FROM 产品,collectionItems,其中 collectionItems.CollectionID = '6' 且 collectionItems.ProductID = Product.barcode)作为 1stResults
其中 typeid = '".$_SESSION['typeid']."' 和 Stoneid = '".$_SESSION['stoneid']."'
按 colorid 升序排序;

如果您不想再次运行第一个查询,请考虑使用临时表来存储结果,然后从中进行查询,就像它是普通表一样。

If this is just in MySQL then you can do

SELECT *
FROM (SELECT * FROM products WHERE onsale =1) AS OnSaleProducts

To use your example,

SELECT distinct colorid from
(SELECT * FROM product, collectionItems where collectionItems.CollectionID = '6' and collectionItems.ProductID = product.barcode) As 1stResults
where typeid = '".$_SESSION['typeid']."' and stoneid = '".$_SESSION['stoneid']."'
order by colorid asc;

If you don't want to run the first query again, consider using a temporary table to store the results, and then querying from that, as if it was a normal table.

灯下孤影 2024-10-31 21:00:51

另外,您可以填充临时表。

例如:

CREATE TEMPORARY TABLE temp_table SELECT * FROM products WHERE onsale = 1

您写道:
其中 recordset1 是 query1 返回的行数。有什么建议吗?谢谢!

行数可以存储在变量中,在这种情况下您不需要结果集。

Also, you could populate temporary table.

For example:

CREATE TEMPORARY TABLE temp_table SELECT * FROM products WHERE onsale = 1

You wrote:
where recordset1 is the number of rows that query1 returned. Any suggestions? Thanks!

The number of rows can be stored in a variable, in this case you don't need resault set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文