Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL-比较一个表中的数据_Mysql_Sql - Fatal编程技术网

Mysql SQL-比较一个表中的数据

Mysql SQL-比较一个表中的数据,mysql,sql,Mysql,Sql,我很难找到一种方法来比较表1中的数据 表1的一部分 Date ID Item ---- ------- ----- 2017-06-30 90 2200 2017-06-30 150 1200 2017-06-30 150 1201 2017-06-30 150 1202 2017-06-30 150 1203 2017-06-30 150 12

我很难找到一种方法来比较表1中的数据

表1的一部分

Date        ID        Item        
----        -------   -----
2017-06-30  90        2200
2017-06-30  150       1200
2017-06-30  150       1201
2017-06-30  150       1202 
2017-06-30  150       1203 
2017-06-30  150       1204
2017-07-31  150       1201
2017-07-31  150       1202
2017-07-31  150       1203
2017-07-31  150       1204
2017-07-31  150       1205
2017-07-31  90        2200
我想得到的结果是1205,因为这是下个月的一个新项目。如果我能得到下个月不再需要的物品,那也太好了,比如1200件

**编辑:我应该提到的一点是,Table1在ID列中也有不同的ID。因此,主要目标是比较确切的ID=150,而不是160或180**

如有任何建议,我将不胜感激


感谢您选择前几个月未包含或前几个月已退役的项目

select 'new item' as result_type, item
from MyTable a1
where not exists
(
select 1
from MyTable a2
where a1.item = a2.item
and a2.Date < a1.date -- change this to a date function to compare to previous month only
)
union all
select 'retired item' as result_type, item
from MyTable a1
where not exists
(
select 1
from MyTable a2
where a1.item = a2.item
and a2.Date > a1.date -- change this to a date function to compare to previous month only
)
例如:


我会把剩下的部分留给读者做练习,但我发现我的计划已经失败了。

如果你想在一个月内同时获得新项目和删除项目:

select 'new', t.*
from t
where not exists (select 1
                  from t t2
                  where t2.item = t.item and
                        year(t2.date) = year(t.date - interval 1 month) and
                        month(t2.date) = month(t.date - interval 1 month)
                 )
union all
select 'deleted', t.*
from t
where not exists (select 1
                  from t t2
                  where t2.item = t.item and
                        year(t2.date) = year(t.date + interval 1 month) and
                        month(t2.date) = month(t.date + interval 1 month)
                 );

@阿德森:我很清楚-谢谢你的回复,我会试着自己来复习。我应该提到的一点是,Table1在ID列中也有不同的ID。所以主要的目标是比较确切的ID=150,而不是160或180。非常感谢!我会试着自己去写并理解它。我还添加了一行代码,在其中我可以选择要检查的确切ID。@michal2805注意,实际上有一种机制可以在上面表达感谢。就这么说吧。
SELECT x.* 
  FROM my_table x 
  LEFT 
  JOIN my_table y  
    ON y.id = x.id AND y.date = x.date - INTERVAL 1 MONTH 
   AND y.item = x.item 
 WHERE x.date = '2017-07-31' 
   AND y.id IS NULL;
select 'new', t.*
from t
where not exists (select 1
                  from t t2
                  where t2.item = t.item and
                        year(t2.date) = year(t.date - interval 1 month) and
                        month(t2.date) = month(t.date - interval 1 month)
                 )
union all
select 'deleted', t.*
from t
where not exists (select 1
                  from t t2
                  where t2.item = t.item and
                        year(t2.date) = year(t.date + interval 1 month) and
                        month(t2.date) = month(t.date + interval 1 month)
                 );