[英]How to use result set from one query in second query on same table
I need to query a table once to obtain ID numbers based on a given condition, then I need to use those ID numbers to retrieve another set of values, all on the same table. 我需要查询一次表以获得基于给定条件的ID号,然后我需要使用这些ID号来检索另一组值,所有这些都在同一个表上。 Here's the first query:
这是第一个查询:
SELECT post_id
FROM ih_postmeta
WHERE meta_key = '_edd_discount_uses'
AND meta_value > '0'
This works fine to retrieve the post_id
numbers. 这适用于检索
post_id
数字。 Now I need to run the equivalent of this query: 现在我需要运行相当于这个查询:
SELECT meta_value
FROM ih_postmeta
WHERE post_id = '1088'
AND meta_key = '_edd_discount_code'
This works, except I need to able to loop through the values obtained from the first query and not have WHERE post_id =
hard coded. 这是有效的,除了我需要能够遍历从第一个查询获得的值并且没有
WHERE post_id =
硬编码。
Here's a sample from the table in question for a specific post_id
: 以下是有关特定
post_id
的表格中的示例:
|meta_id|post_id| meta_key |meta_value
|1822 |1088 |_edd_discount_uses|8
|1823 |1088 |_edd_discount_code|ls100
You can use IN with a subquery to get what you want: 您可以将IN与子查询一起使用以获得所需内容:
SELECT meta_value
FROM ih_postmeta
WHERE post_id IN (SELECT post_id
FROM ih_postmeta
WHERE meta_key = '_edd_discount_uses'
AND meta_value > '0')
AND meta_key = '_edd_discount_code'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.