![](/img/trans.png)
[英]Update multiple rows of a table having specific value in particular column equal to value calculated from two other tables
[英]SQL Select from 1 table rows with 2 specific column value that are not equal
我有桌子
id number name update_date
1 123 asd 08.05.18
2 412 ddd 08.05.18
3 123 dsa 14.05.18
4 125 dsa 05.05.18
整个表都由这样的行组成。 我需要选择第1行和第3行,因为我需要不同的update_dates但编号相同。 怎么做? 我需要查看2个更新日期08.05.18和14.05.18之间特定编号的更改。 我的表格中有更多更新日期。
我试过了:
SELECT *
FROM legal_entity_history a
JOIN legal_entity_history b ON a.BIN = b.BIN
WHERE ( a.update_date <> b.update_date AND
a.update_date = "08.05.18" AND
b.update_date = "14.05.18" )
尝试一下:假设相同的数字在相同的update_date
下可能不会出现多次,因此,您可以使用GROUP BY
和HAVING
来实现,如下所示
SELECT t.*
FROM test t
INNER JOIN (SELECT number
FROM test
GROUP BY number
HAVING COUNT(DISTINCT update_date) > 1) t1 ON t1.number = t.number
输出 :
id number name update_date
1 123 asd 08.05.18
3 123 dsa 14.05.18
一个相对简单的方法是:
select leh.*
from legal_entity_history leh
where exists (select 1
from legal_entity_history leh2
where leh2.number = leh.number and leh2.update_date <> leh.update_date
);
为了提高性能,您需要在legal_entity_history(number, update_date)
上建立索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.