简体   繁体   English

具有两个联接和简单逻辑的sql查询

[英]sql query with two joins and a simple logic

Please find the fiddle http://sqlfiddle.com/#!9/4a4aff/7 请找到小提琴http://sqlfiddle.com/#!9/4a4aff/7

Friends_list Table Friends_list

 | 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. 从这两个表中,我希望列出所有用户,而在好友列表中的状态不是P或B的用户,使用联接似乎很简单,但是在wordpress中,此代码不起作用,查询如下。

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')

You must join tables with user id like below: 您必须使用以下用户ID联接表:

LEFT JOIN friends_list f ON f.user_id =u.ID 左加入friends_list f ON f.user_id = u.ID

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

Add two records at friends_list table with Q and Z status for testing friends_list表中添加两个记录,分别显示QZ状态以进行测试

INSERT INTO `friends_list` (`id`, `user_id`, `Friend_id`, `status`) VALUES
(80, 1, 26, 'P'),
(79, 18, 26, 'P'),
(78, 1, 24, 'P'),
(79, 20, 24, 'Q'),
(80, 21, 24, 'Z');

sqlfiddle working example sqlfiddle工作示例

From what I understand you want ALL user except the those who has P or B as status. 据我了解,您希望除具有P或B作为身份的用户之外的所有用户。 To get those users that don't have a value in the friends_list table at all you can add a null check to your query. 要获得在friends_list表中根本没有值的那些用户,您可以向查询添加空检查。

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

I'm also unsure if you're joining on the right column? 我也不确定您是否要加入右栏? Should it be user_id instead of friend_id? 应该是user_id而不是friend_id吗?

Just change the binding column like this :- 只需像这样更改绑定列:-

select DISTINCT u.ID, u.user_nicename from wp_users u
LEFT JOIN friends_list f ON f.user_id=u.ID
WHERE f.status NOT IN ('P', 'B');

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

You can use this query, your fiddle is showing blank result as you have only data with P Status, try to add one more entry with some other status then you will get the result. 您可以使用此查询,您的小提琴显示空白结果,因为您只有P状态的数据,尝试再添加一个具有其他状态的条目,您将得到结果。

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 is NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM