MySQL:如何对现有记录集执行查询?
我想对现有记录集执行 SELECT
查询,例如:
query1 = 'Select * from products where onSale = 1'
然后其中
query2 = 'Select * from recordset1 where ....'
recordset1
是 query1
返回的行数。 有什么建议吗?谢谢!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用子查询:
Use a subquery:
如果这只是在 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.
另外,您可以填充临时表。
例如:
您写道:
其中 recordset1 是 query1 返回的行数。有什么建议吗?谢谢!
行数可以存储在变量中,在这种情况下您不需要结果集。
Also, you could populate temporary table.
For example:
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.