所以我有这个数据库表有一些记录,我想只选择一行,如果它与以前基于几列(C1,C2,C3)不同..忽略日期和所有其他列..我想检查这3列,如果它与以前不同,只带一行。需要一些帮助。
Name C1 C2 C3 Date
Test Y Y Y 10/23/2017 0:00:00
Test Y Y Y 10/24/2017 0:00:00
Test N Y Y 10/25/2017 0:00:00
Test Y Y N 10/26/2017 0:00:00
Test Y Y N 10/27/2017 0:00:00
Test Y Y Y 10/28/2017 0:00:00
Test Y Y Y 10/30/2017 0:00:00
我想要的结果应该是这样的:
Name C1 C2 C3 Date
Test Y Y Y 10/23/2017 0:00:00
Test N Y Y 10/25/2017 0:00:00
Test Y Y N 10/26/2017 0:00:00
Test Y Y Y 10/28/2017 0:00:00
答案 0 :(得分:2)
假设您将日期存储为日期,这里有一个想法......
SELECT a.*
FROM my_table a
LEFT
JOIN
( SELECT x.*
, MIN(y.date) next
FROM my_table x
LEFT
JOIN my_table y
ON y.name = x.name
AND y.date > x.date
GROUP
BY x.Name
, x.Date
) b
ON b.Name = a.Name
AND b.next = a.date
AND b.c1=a.c1
AND b.c2=a.c2
AND b.c3 = a.c3
WHERE b.Name IS NULL;
答案 1 :(得分:1)
我认为这可以满足您的需求:
select t.*
from t
where (t.c1, t.c2, t.c3) <>
(select t2.c1, t2.c2, t2.c3
from t t2
where t2.name = t.name and t2.date < t.date
order by t2.date desc
limit 1
);
答案 2 :(得分:1)
SELECT
*
FROM
(SELECT
p.CC,
p.C1,
p.C2,
p.C3,
p.C4,
CASE
WHEN @prev_value = CC THEN @rank_count
WHEN @prev_value:=CC THEN @rank_count:=@rank_count + 1
END AS rank
FROM
(SELECT
CONCAT(C2, C3, C4) AS CC, t.*
FROM
mytbl t) p, (SELECT @rank_count:=0, @prev_value:=NULL) r) q
WHERE
q.rank IS NULL
感谢。 BR, 穆罕默德。