[英]MySQL Get Matching Results From two queries?
I have some php code that generates multiple MySql queries and combines the results. 我有一些PHP代码生成多个MySql查询并结合结果。 I am trying to find a way to combine the queries so the only fields that match in both queries are outputed.
我试图找到一种方法来组合查询,以便在两个查询中匹配的唯一字段被输出。
Example of PHP Codes Output. PHP代码输出示例。
SELECT LoadData_3 pow FROM tblLoadData WHERE LoadData_1 = 'test1' AND LoadData_2 = 'test2'
UNION ALL
SELECT LoadData_3 pow FROM tblLoadData WHERE LoadData_1 = 'test3' AND LoadData_2 = 'test4'
I want only the LoadData_3 fields that are the same in both queries. 我只想要两个查询中相同的LoadData_3字段。
Use the INTERSECT
operation: 使用
INTERSECT
操作:
(SELECT LoadData_3 pow
FROM tblLoadData
WHERE LoadData_1 = 'test1'
AND LoadData_2 = 'test2')
INTERSECT -- return the intersection of the two queries
(SELECT LoadData_3 pow
FROM tblLoadData
WHERE LoadData_1 = 'test3'
AND LoadData_2 = 'test4')
EDIT 编辑
For MySQL you can use INNER JOIN USING (..)
对于MySQL,您可以使用
INNER JOIN USING (..)
SELECT pow -- Use DISTINCT pow instead if you want unique values (like INTERSECT)
FROM (SELECT LoadData_3 pow
FROM tblLoadData
WHERE LoadData_1 = 'test1'
AND LoadData_2 = 'test2') T1
INNER JOIN
(SELECT LoadData_3 pow
FROM tblLoadData
WHERE LoadData_1 = 'test3'
AND LoadData_2 = 'test4') T2
USING (pow)
Or using a WHERE IN clause: 或者使用WHERE IN子句:
SELECT LoadData_3 pow -- Use DISTINCT pow instead if you want unique values (like INTERSECT)
FROM tblLoadData
WHERE LoadData_1 = 'test1'
AND LoadData_2 = 'test2'
AND LoadData_3 IN (SELECT LoadData_3 pow
FROM tblLoadData
WHERE LoadData_1 = 'test3'
AND LoadData_2 = 'test4')
Big Edit! 大编辑!
I've spent too much time in Oracle, and I skimmed over that article too quickly! 我在Oracle中花了太多时间,而且我太快地浏览了那篇文章! The above answer was edited before mine.
上面的答案在我之前编辑过。 You can also take a look at the following question:
您还可以查看以下问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.