在同一个表中比较两个查询集结果的行

时间:2015-08-07 06:48:53

标签: php mysql

我有基于日期存储数据的表格。

现在我需要检查一下:具有两个不同日期的行数据之间是否存在任何差异。

以简单的方式,您可以说我有两个查询从同一个表中选择数据,现在我必须比较每个行和列的值。例如我的两个查询是 -

SELECT *  FROM `national` WHERE `upload_date` = '2015-08-04'  // return 106 rows

SELECT *  FROM `national` WHERE `upload_date` = '2015-08-01'   // return 106 rows

我试图将此与下面的查询进行比较,但结果对我来说似乎不正确,我对此并不满意。

Select U.* from 
(SELECT t1.* FROM `national` t1 where upload_date = '2015-08-01'
union all
SELECT t2.*  FROM `national` t2 WHERE `upload_date` = '2015-08-04' ) U
GROUP BY emp_id, uqi_id
HAVING COUNT(*) = 1

任何人都可以提供正确的查询吗?谢谢

2 个答案:

答案 0 :(得分:2)

试试这个

 (
  SELECT t1.* 
  FROM 
     `national` t1, `national` t2 
  where
      t1.upload_date = '2015-08-01' and t2.upload_date='2015-08-04' and
      -- put your columns here that you want to compare for same DATA
      -- like t1.name=t2.name and etc...
 ) 

答案 1 :(得分:0)

你可以尝试类似的东西

SELECT * FROM (SELECT * FROM `national` WHERE upload_date = '2015-08-01') a
INNER JOIN (SELECT * FROM `national` WHERE upload_date = '2015-08-04') b
     ON a.emp_id = b.emp_id AND a.uqi_id = b.uqi_id
ORDER BY uqi_id