简体   繁体   English

如何在另一张桌子上不存在

[英]How to not exist on another table

How to not exist on another table on this two table? 如何在这两个表的另一个表上不存在?

Users table 在此输入图像描述

User_relationships table

在此输入图像描述

and this is my current query 这是我目前的查询

"SELECT * FROM `users` 
WHERE CONCAT(first_name, " ", last_name) LIKE '%' . $name . '%' 
    AND (`role_id` = 6 OR `role_id` = 4)
ORDER BY `first_name` asc limit 15"

and I want to add on query where guardian_id(user_id) not exist on the user_relationships table 我想添加查询,其中user_relationships表上不存在guardian_id(user_id)

UPDATE 1 更新1

select * from `users` 
    where not exists 
            (select 1 from `user_relationships` 
             inner join `users` on `user_relationships`.`guardian_id` = `users`.`id` 
             where `user_relationships`.`student_id` = 422) 

I tried this and still returns me zero result. 我试过这个但仍然给我零结果。

I only have var name = ? 我只有var name = ? and student_id = ? student_id = ?

try to use "NOT IN" query function with 'DISTINCT'. 尝试使用“DISTINCT”使用“NOT IN”查询功能。

following may help you. 以下可能对您有帮助。

select * from users where CONCAT(first_name, " ", last_name) LIKE '%' . $name . '%' and (role_id= 6 or role_id= 4) and (id NOT IN (select DISTINCT guardian_id from User_relationships where student_id = $student_id)) order by first_name ASC limit 15

Let me know if you still need some changes. 如果您仍需要进行一些更改,请与我们联系。

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

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