有人可以帮我解决这个问题。
我有两张桌子:
TASKS:(id,once)
SAVED_tasks (id, task_id)
我需要显示所有任务,但是如果任务的值为1并且已经插入SAVED_tasks表中,则不需要显示任何任务。
实施例
任务:
id, name, once
1, task1, 0
2, task2, 1
3, task3, 0
4, task4, 1
saved_tasks:
id, task_id
1, 1
2, 2
3, 3
4, 4
我需要结果:
1, task1
3, task3
答案 0 :(得分:2)
SELECT TASK.id, SAVED_tasks.task_id FROM TASKS Inner join SAVED_tasks ON TASK.id = SAVED_tasks.id
AND TASK.once > 1
答案 1 :(得分:1)
如果你需要值,如果你的SAVED_task表中有数据,那么尝试连接两个表!如下所示!= 1:
SELECT t.id
FROM TASKS t INNER JOIN SAVED_tasks st
ON t.id = st.id
WHERE t.once != 1
答案 2 :(得分:1)
应该这样做:
Select * from TASKS
Where TASKS.id Not In (Select task_id from SAVED_TASKS)
and TASK.once != 1
答案 3 :(得分:1)
试试这个:
SELECT t.id, t.once
FROM TASKS t
LEFT JOIN SAVED_tasks st ON t.id = st.task_id
WHERE (t.once != 1 OR (t.once = 1 AND st.id IS NULL));
或
SELECT t.id, t.once
FROM TASKS t
LEFT JOIN SAVED_tasks st ON t.id = st.task_id
WHERE NOT(t.once = 1 AND st.id IS NULL);