dongwei3866 2017-11-14 09:08
浏览 71
已采纳

sql查询有两个连接和一个简单的逻辑

Please find the fiddle http://sqlfiddle.com/#!9/4a4aff/7

Friends_list Table

 | id | user_id | Friend_id | status
 |  1 |   1     |    24     |   P
 |  2 |   18    |    26     |   P

User Table

| ID | email   | password  |  ..N
 |  1 |   xx    |    xx     |   xx
 |  2 |   xx    |    xx     |   xx

from those both table i want to get all the users listed without the users whose status was P or B in friends list table it seems to be simple using joins but in wordpress this code is not working and the query is as below.

select DISTINCT u.ID, u.user_nicename from wp_users u
LEFT JOIN friends_list f ON f.friend_id=u.ID
WHERE f.status  NOT IN ('P', 'B')
  • 写回答

5条回答 默认 最新

  • dplo59755 2017-11-14 09:20
    关注

    Change your query as below. If you only want data from first table then no need join query only use inner query

    select DISTINCT u.ID, u.user_nicename from wp_users u WHERE u.ID not in 
    (select f.friend_id from friends_list f where f.status IN ('P', 'B')) order by u.ID;
    

    Fiddle

    EDIT

    select DISTINCT u.ID, u.user_nicename from wp_users u
    LEFT JOIN friends_list f ON f.friend_id=u.ID
    WHERE f.status NOT IN ('P', 'B') OR f.status IS NULL ORDER BY u.ID
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程