基本上我有2个表:uclk_user
和uclk_task
uclk_user
表格有user_id
,name
,postcode
uclk_task
表格包含user_id
,job_id
,description
,fully_completed
,date_due
我想在下面的任务查询中包含uclk_user
中的用户名...
显然user_id是关键。
select job_id, user_id, fully_completed, description, date_due
from uclk_task
WHERE date_due <= NOW() AND fully_completed = 0
ORDER BY date_due ASC
应如何做到这一点?
答案 0 :(得分:6)
使用JOIN
从通用条件的多个表中进行选择
SELECT t.job_id, t.user_id, t.fully_completed, t.description, t.date_due, u.name
FROM uclk_task t
INNER JOIN uckl_user u
ON t.user_id = u.user_id
WHERE t.date_due <= NOW() AND t.fully_completed = 0
ORDER BY t.date_due ASC
答案 1 :(得分:1)
试试这个
select job_id, user_id, fully_completed, description, date_due
from uclk_task
Left join
on date_due <= NOW() AND fully_completed = 0
ORDER BY date_due ASC