我在构建查询时遇到问题,我在“未知列”上遇到错误,j.id在on子句中。
这是我目前的查询
SELECT
j.id,
j.title,
venues.name as venueName,
ja.completed
FROM
dbsivcmsnew.jobs AS j,
venues
LEFT JOIN dbsiv.job_applications AS ja ON ja.jobId = j.id
WHERE
j.venueId = venues.id
AND j.closingDate > UNIX_TIMESTAMP()
AND j.active = 1
ORDER BY
j.closingDate DESC
从here我可以看到,语法是正确的。
但是,JOIN可能是不必要的,我的查询需要返回的是
SELECT
j.id,
j.title,
venues.name as venueName,
ja.completed
FROM
jobs AS j,
venues,
dbsiv.job_applications AS ja
WHERE
j.venueId = venues.id
AND j.closingDate > UNIX_TIMESTAMP()
AND j.active = 1
AND ja.pin = $CurrentUsersId //This needs to optional though, if no match ja.completed should be 0
ORDER BY
j.closingDate DESC
是否可以在不使用连接的情况下使WHERE语句可选?
答案 0 :(得分:2)
好吧,你为" table join"混合了语法,这是......不是一个好主意
只需在LEFT JOIN中添加你的附加条款即可。
SELECT
j.id,
j.title,
v.name as venueName,
ja.completed
FROM
jobs j
inner join venues v on v.id = j.venueID
left join dbsiv.job_applications ja on ja.jobId = j.id and ja.pin = $CurrentUsersId
WHERE
AND j.closingDate > UNIX_TIMESTAMP()
AND j.active = 1
ORDER BY
j.closingDate DESC
答案 1 :(得分:0)
将您的选择更改为:
SELECT
j.id,
j.title,
venues.name as venueName,
ja.completed
FROM
dbsivcmsnew.jobs AS j join j.venueId = venues.id
venues on
LEFT JOIN dbsiv.job_applications AS ja ON ja.jobId = j.id
WHERE
j.closingDate > UNIX_TIMESTAMP()
AND j.active = 1
ORDER BY
j.closingDate DESC
如果您使用join,则将其用于from statement
中的所有表答案 2 :(得分:0)
由于您指定INNER JOIN的顺序,在连接条件下无法访问列j.id.我重新安排了订单,如下所示:
SELECT
j.id,
j.title,
venues.name as venueName,
ja.completed
FROM
VENUES
INNER JOIN dbsivcmsnew.jobs AS j ON j.venueId = venues.id
LEFT JOIN dbsiv.job_applications AS ja ON ja.jobId = j.id
WHERE j.closingDate > UNIX_TIMESTAMP()
AND j.active = 1
ORDER BY J.CLOSINGDATE DESC;