douxiong5972 2013-01-27 10:22
浏览 34
已采纳

MySQL显示了尚未选择的成员

We offer some bonuses for members to choose from and I want to show a popup with the bonuses they have not already selected. bonus contains info on the different bonuses, and selected_bonuses contains bonus id and the members id.

SELECT bonus.* FROM bonus 
LEFT JOIN selected_bonuses 
ON bonus.id = selected_bonuses.bonus_id 
WHERE selected_bonuses.member_id = 'xxxx'

This will show the bonuses selected for member 'xxxx'.

SELECT bonus.* FROM bonus 
LEFT JOIN selected_bonuses 
ON bonus.id = selected_bonuses.bonus_id 
WHERE selected_bonuses.member_id IS NULL

This show the bonuses not selected by any member. I want to show what bonuses just member 'xxxx' have not chosen.

Any suggestions?

  • 写回答

2条回答 默认 最新

  • duanmei1350 2013-01-27 10:29
    关注

    With your query

    SELECT bonus.* FROM bonus 
    LEFT JOIN selected_bonuses 
    ON bonus.id = selected_bonuses.bonus_id 
    WHERE selected_bonuses.member_id = 'xxxx'
    

    you join information about each bonus to each member having selected that bonus, then select only one member.

    If you instead join each bonus to just the member you are interested in, and then select the bonuses for which no such member exists, you should get what you want:

    SELECT bonus.* FROM bonus 
    LEFT JOIN selected_bonuses 
    ON bonus.id = selected_bonuses.bonus_id 
    AND selected_bonuses.member_id = 'xxxx'
    WHERE selected_bonuses.member_id IS NULL
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计