如何将其转换为使用JOINS?
SELECT j.job_id, j.name AS job_name, a.name AS advertiser_name, j.time_added, j.active, j.moderated
FROM jobs j, advertisers a
WHERE a.advertiser_id = j.advertiser_id
答案 0 :(得分:6)
您可以使用以下连接来编写:
SELECT *
FROM jobs j
JOIN advertisers a
ON a.advertiser_id = j.advertiser_id
答案 1 :(得分:2)
详细说明JOINS这里有几个例子:
你的例子是一个内部联接 - 但是,你使用“隐式连接符号”这里是明确表示这一点的方法:
SELECT j.job_id, j.name AS job_name, a.name AS advertiser_name,
j.time_added, j.active, j.moderated
FROM jobs j
INNER JOIN advertisers a
ON j.advertiser_id = a.advertiser_id
根据您的预期结果,您可以选择使用OUTER JOIN。与INNER JOIN的区别在于INNER JOIN只会查找在另一个表中具有匹配记录的记录。相反,如果您希望作业表或广告商表中的记录在另一个表中没有相应的记录时使用OUTER JOIN,则可以使用OUTER JOIN。
在此示例中,查询将查找jobs表中的所有记录,而不管广告商表中是否匹配:
SELECT j.job_id, j.name AS job_name, a.name AS advertiser_name,
j.time_added, j.active, j.moderated
FROM jobs j
LEFT OUTER JOIN advertisers a
ON j.advertiser_id = a.advertiser_id
但是,无论作业表中的匹配项如何,此查询都将查找广告商表中的所有记录:
SELECT j.job_id, j.name AS job_name, a.name AS advertiser_name,
j.time_added, j.active, j.moderated
FROM jobs j
RIGHT OUTER JOIN advertisers a
ON j.advertiser_id = a.advertiser_id
LEFT或RIGHT对应于表格所在的'='的哪一侧。
FULL OUTER JOIN将返回每个表中的所有记录,无论匹配如何:
SELECT j.job_id, j.name AS job_name, a.name AS advertiser_name,
j.time_added, j.active, j.moderated
FROM jobs j
FULL OUTER JOIN advertisers a
ON j.advertiser_id = a.advertiser_id
答案 2 :(得分:1)
SELECT j.job_id, j.name AS job_name, a.name AS advertiser_name, j.time_added, j.active, j.moderated from
jobs j join advertisers a
on a.advertiser_id = j.advertiser_id