dps43378 2011-09-06 12:18 采纳率: 0%
浏览 8
已采纳

跨两个表构建一个mysql查询

I have a table job_to_universities with the fields

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

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.

  • 写回答

4条回答 默认 最新

  • douchi7073 2011-09-06 12:24
    关注

    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:

    SELECT ... FROM job_postings WHERE is_public = 1
    UNION DISTINCT
    SELECT ... FROM job_postings NATURAL JOIN job_to_universities
    WHERE univ_id = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致