Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

mysql - How get results only if the endDate NOT greater than today

I need only the ids where the endDate is not greater than today or all endDates of the id are expired

table 1 t1

id | 
---
1
2
3
4

table 2 t2

id | t1_id | endDate
-----------------------
1  |   2   | 2019-01-01
2  |   3   | 2019-01-01
3  |   3   | 2020-01-01
4  |   3   | 2025-01-01

Query

SELECT t1.id ,t2.endDate 
FROM table_1 t1
    LEFT JOIN table_2 t2 ON t2.t1_id = t1.id
WHERE NOT(t2.endDate > CURDATE())
AND ???

I need this result:

t1.id | t2.endDate
-----------------------
2     | 2019-01-01
 
question from:https://stackoverflow.com/questions/65644741/how-get-results-only-if-the-enddate-not-greater-than-today

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand your requirement correctly you can use NOT EXISTS:

SELECT t1.id, t2.endDate 
FROM table_1 t1
INNER JOIN table_2 t2 ON t2.t1_id = t1.id
WHERE NOT EXISTS 
(
  SELECT 1
  FROM table_2 t
  WHERE t.t1_id = t2.t1_id AND t.endDate > CURDATE()
)  

Demo here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...