dongsi4547 2016-04-29 07:54
浏览 89
已采纳

如何获取用户id不在表记录mysql中的选定行

How get selected rows where user id is not in table record

the table like this

table_a

id subid userid
1  2     123
2  4     123

table_b

id title
1  like
2  liked
3  bookmark
4  bookmarked

i use join table to get the result if WHERE userid=123 then get result

id title
2  liked
4  bookmarked

the query

select b.id,b.title from
table_a a
LEFT JOIN table_b b
ON b.id = a.subid
WHERE a.userid = '123'

but, how if userid = '345' and not in table_a get the result too?.. the result is must like this (selected rows)

id title
1  like
3  bookmark

Thankyou

  • 写回答

4条回答 默认 最新

  • douya2433 2016-04-29 08:03
    关注

    You can use the following query:

    SELECT id, title FROM t2 LEFT JOIN t1 ON t1.subid = t2.id 
    WHERE (t1.userid = 345 AND NOT userid IS NULL) OR (
        NOT EXISTS(SELECT userid FROM t1 WHERE userid = 345) AND userid IS NULL
    );
    

    The working examples you can find here: http://sqlfiddle.com/#!9/c6a446/25

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路