简体   繁体   中英

MYSQL Join From Two Tables

I am not sure what I am missing but I know it should not be this hard. I have two tables. First table lists all the possible jobs for a site. The second table has when a job was completed at a site. I want a query where by I get all the possible jobs and join in the data from the seconds table should there be some. Site that have not completed the job would have a NULL value.

Example:

Table 1:
ID
name
decsription

Table 2:
ID
siteID
status
jobID

SELECT table1.* , table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.jobID
WHERE siteID = 12

This only returns jobs that have been completed and not all jobs completed and uncompleted.

Thanks in advance for your help.

如果需要此特定站点,则应删除where语句(当第二个表中的行为emty时,它不满足siteID = 12的条件)或进行更改(添加“ OR siteID is NULL”)。

A simple change should do, just change the order of the values on the JOIN , like this:

SELECT table1.* , table2.* 
FROM table1
LEFT JOIN table2
ON table2.jobID = table1.id
WHERE siteID = 12 OR siteID IS NULL

And keep in mind the siteID will be NULL when there are no results, so change your WHERE as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM