我有一个带有字段的表job_to_universities
id univ_id post_id
1 2 3
2 3 3
3 5 5
4 1 8
5 2 8
我有另一个表job_postings与字段
id(post_id) is_public
1 1
2 0
3 1
4 1
5 1
6 0
7 0
8 0
对于5的大学ID,我想从job_postings表中获取所有作业以及job_postings表中is_public为1的作业,我仍然坚持这一点。
答案 0 :(得分:1)
SELECT jp.post_id
FROM job_postings jp, job_to_universities ju
WHERE jp.id = ju.post_id AND ju.univ_id = 5 AND jp.is_public = 1
答案 1 :(得分:1)
如果我理解正确,您希望获得所有公开和与大学相关的职位发布。这意味着一个相当简单的连接:
SELECT ... FROM job_postings NATURAL JOIN job_to_universities
WHERE univ_id = ? AND is_public = 1
如果您希望获得所有公开或与大学相关的职位发布,您可以写下以下内容:
SELECT ... FROM job_postings WHERE is_public = 1
OR post_id IN (SELECT post_id FROM job_to_universities WHERE univ_id = ?)
或使用UNION DISTINCT
:
SELECT ... FROM job_postings WHERE is_public = 1
UNION DISTINCT
SELECT ... FROM job_postings NATURAL JOIN job_to_universities
WHERE univ_id = ?
答案 2 :(得分:0)
类似的东西:
SELECT b.id FROM job_to_universities a , job_postings b
WHERE a.post_id = b.id AND b.is_public = 1 AND a.univ_id = 5
答案 3 :(得分:0)
试试这个:
SELECT *
FROM job_postings b
WHERE
(
id IN (SELECT post_id FROM job_to_universities WHERE univ_id = 5)
OR
is_public = 1
)