SQL查询 - 从另一个表中获取名称

时间:2012-08-26 11:31:49

标签: mysql

基本上我有2个表:uclk_useruclk_task

uclk_user表格有user_idnamepostcode

uclk_task表格包含user_idjob_iddescriptionfully_completeddate_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

应如何做到这一点?

2 个答案:

答案 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