[英]framing a mysql query across two tables
I have a table job_to_universities with the fields 我有一个表job_to_universities与字段
id univ_id post_id
1 2 3
2 3 3
3 5 5
4 1 8
5 2 8
I have another table job_postings with the fields 我有另外一个表job_postings与字段
id(post_id) is_public
1 1
2 0
3 1
4 1
5 1
6 0
7 0
8 0
say for an university id of 5 i want to get all the jobs from the job_postings table along with the jobs that have is_public as 1 in the job_postings table and i am stuck on this. 对于5的大学ID,我想从job_postings表中获取所有作业以及job_postings表中is_public为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
If I understand you correctly, you wish to get all job postings that are both public and related to an university. 如果我理解正确,您希望获得所有公开和与大学相关的职位发布。 This would imply a fairly simple join:
这意味着一个相当简单的连接:
SELECT ... FROM job_postings NATURAL JOIN job_to_universities
WHERE univ_id = ? AND is_public = 1
If you wish to get all job postings that are either public or related to an university, you can write something along the lines of: 如果您希望获得所有公开或与大学相关的职位发布,您可以写下以下内容:
SELECT ... FROM job_postings WHERE is_public = 1
OR post_id IN (SELECT post_id FROM job_to_universities WHERE univ_id = ?)
Or use UNION DISTINCT
: 或者使用
UNION DISTINCT
:
SELECT ... FROM job_postings WHERE is_public = 1
UNION DISTINCT
SELECT ... FROM job_postings NATURAL JOIN job_to_universities
WHERE univ_id = ?
something like : 就像是 :
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
Try this: 尝试这个:
SELECT *
FROM job_postings b
WHERE
(
id IN (SELECT post_id FROM job_to_universities WHERE univ_id = 5)
OR
is_public = 1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.