[英]SQL Query - Get Name from another table
基本上我有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
應該怎么做?
使用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
試試這個
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.