使用一个表选择两行之间的差异

时间:2016-08-05 05:48:00

标签: mysql sql sql-server database

我有table-A,我想从2016-06-252016-06-26中选择日期范围来查找名称的差异。

根据图片,结果应该是这样的:

2016-06-26  joe     40

我猜查询有点像这样:

SELECT * FROM `2g`
WHERE  'Date' between '2016-06-25' AND 'Date'   '2016-06-26'  
and (`name` != `name`)

enter image description here

3 个答案:

答案 0 :(得分:0)

此where and ( name != name )子句永远不会返回任何内容,因为name始终等于name。你应该做点什么:

SELECT * FROM 2g WHERE 'Date' between ('2016-06-25' AND '2016-06-26')  
AND count(name) = 1  
GROUP BY name

答案 1 :(得分:0)

请检查一下。我不知道这个语法适用于mysql。

SELECT name FROM 2g
WHERE  'Date' between '2016-06-25' AND '2016-06-26'  
Group by Name 
Having count(Name) > 1

答案 2 :(得分:0)

请使用我的SQL查询希望这项工作适合您。

with x as   (select  *,k = COUNT(*)
            over(PARTITION BY Name,Age )
            from 2g)

select [Date]
      ,[Name]
      ,[Age] from x
where k <= 1 and Date between '2016-06-25' AND '2016-06-26' 

enter image description here