[英]How get selected rows where user id is not in table record mysql
How get selected rows where user id is not in table record 如何获取表记录中没有用户ID的选定行
the table like this 像这样的桌子
table_a table_a
id subid userid
1 2 123
2 4 123
table_b 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 我使用连接表来获取结果,如果WHERE userid = 123则获取结果
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) 但是,如果userid ='345'并且不在table_a中也得到结果吗?..结果必须是这样的(选定的行)
id title
1 like
3 bookmark
Thankyou 谢谢
尝试使用此查询从(b.no = a.no)的连接b上选择a.no,b.title,其中a.userid ='345'
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 您可以在这里找到工作示例: http : //sqlfiddle.com/#!9/c6a446/25
You can use left join and get the data for the table where their is no match. 您可以使用左连接并获取不匹配的表的数据。 I have solved this recently.
我最近解决了这个问题。 Chk this link: Least Popular Event - Get list of events where bookings IS NULL OR LEAST
单击此链接: 最受欢迎事件-获取预订为空或最少的事件列表
In mysql 在mysql中
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid IS NULL;
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid <> '345';
in laravel : $least = bmodel::leftJoin('a', function($join) { $join->on('a.subid', '=', 'b.no'); }) ->select('b.no','b.title') ->whereNull('a.userid') ->first(); 在laravel中:$ least = bmodel :: leftJoin('a',function($ join){$ join-> on('a.subid','=','b.no');})-> select( 'b.no','b.title')-> whereNull('a.userid')-> first(); or get()
或get()
You can use this query to get one or more results. 您可以使用此查询来获得一个或多个结果。 It basically compares userid column with no column and gets list of no and title where there is no match in table a.
它基本上比较userid列和no列,并在表a中没有匹配项的情况下获取no和title的列表。
This basically would select with given userId or if that is not present it then selects the result which
这基本上将使用给定的userId进行选择,或者如果不存在,则选择结果
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 123||
(select userid from t1 where userid =123LIMIT 1) is null
This is the second case where you will get all the rows where it is not 123 or any number 这是第二种情况,您将获得所有不是123或任何数字的行
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 345 ||
(select userid from t1 where userid =345 LIMIT 1) is null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.