[英]SQL Query to select records based on 2 different values in the same field and a condition
[英]Select a field only if no other records with the same field value meet a condition
如果下面的查询返回分配给尚未启动的报告的任何模板的“模板” ID ...
我还能怎么说(与每个模板ID对应-这个模板ID不能出现在任何其他已经开始的报表上)。
或者,我的意思是我正在尝试完成下面的查询,因此它显示为“给我所有已开始使用的报告未使用该模板的所有模板的ID”。 就像是否对每个t.id一样,我遍历了该表,确保没有其他带有template_id(t.id)的报告已经开始。
SELECT DISTINCT t.id,
FROM templates t
LEFT OUTER JOIN reports r ON r.template_id = t.id
WHERE r.start_time >= UTC_TIMESTAMP()
谢谢!
您可以使用类似的子查询
SELECT DISTINCT t.id,
FROM templates t
LEFT OUTER JOIN reports r ON r.template_id = t.id
WHERE r.start_time >= UTC_TIMESTAMP() and t.id not in (select CONCAT_WS(',',tid) where report started)
我不完成子查询,因为我不知道确切的表结构。 因此,相应地完成where子句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.