2017-03-06 70 views
0

我想进行一个查询,从表中选择所有行,除了其他表中具有相同标识的行用户,并在另一列中具有相同的值。从表中选择所有行,除了在另一个表中具有相同ID的行在另一个列中具有特定值的位置

Table1 
id | value 
================= 
1  | 10 
2  | 20 
3  | 30 
4  | 40 
5  | 50 
6  | 60 

-

Table2 
id  | user_ids | another_column 
========================================= 
1  |  2  |  30 
2  |  4  |  50 
3  |  4  |  60 

所以如果Table2.user_ids =(用户ID的loggedIn),并在同一时间Table1.value = Table2.another_column - 那些行不应在结果中ouputted。

如果说,我们有ID的用户:4,用户不应该看到ROW5或ROW6从表1,因为值匹配

我应该去一些类型的子查询或加入的SQL?

+1

见http://meta.stackoverflow.com/questions/333952/why-should- i-provide-an-mcve-for-what-seems-to-the-very-simple-sql-query – Strawberry

回答

0

以ID为4的用户从你上面的例子:

select * from Table1 
where value not in (
    select another_column 
    from Table2 
    where user_ids = 4 
); 
+0

谢谢!这解决了我的问题 – rkdbrors

0
select id, value from t1 where value not in (select another_column from t 2 where userid = 4) 

类似的东西

+0

谢谢你这个作品 – rkdbrors

相关问题