Table 1 条记录不应出现在 Table 2 中
Table 1 record should not be present in Table 2
我在 mysql:
的同一个数据库中有两个 table
- Table 1
- Table 2
我正在使用 phpmyadmin 和 php。
我在 table 和 table 中都有列 'teamid' 1 包含几个不同的事件列。
所以我从table1.
中抽出了参加特定活动的teamid
我在 table2 中也有参加过特定活动的专栏 teamid。所以我也从table2.
中拉出了参加特定活动的teamid
查询 1:Select teamid 来自 table1
where event1pay='Paid'
查询 2:Select teamid From table2
where event='event1'
所以从查询1和查询2中,我取出了teamid。
Table1:有55条记录
Table2:有5条记录。
我想要table1条teamid记录不应该出现在table2条teamid记录中。
所需的查询应 return 50 条记录。
我已经应用了 Left Join, NOT IN 但它不起作用,因为上述两个查询具有不同的 where 子句。
我会推荐 not exists
:
select *
from table1 t1
where event1pay = 'Paid' and not exists (
select 1
from table2 t2
where t2.event = 'event1' and t2.teamid = t1.teamid
)
如果你想用反left join
来做到这一点,那就是:
select t1.*
from table1 t1
left join table2 t2 on t2.teamid = t1.teamid and t2.event = 'event1'
where t1.event1pay = 'Paid' and t2.teamid is null
但我发现 not exists
是表达您想在这里做的事情的更好方式。
我在 mysql:
的同一个数据库中有两个 table- Table 1
- Table 2
我正在使用 phpmyadmin 和 php。
我在 table 和 table 中都有列 'teamid' 1 包含几个不同的事件列。 所以我从table1.
中抽出了参加特定活动的teamid我在 table2 中也有参加过特定活动的专栏 teamid。所以我也从table2.
中拉出了参加特定活动的teamid查询 1:Select teamid 来自 table1
where event1pay='Paid'
查询 2:Select teamid From table2
where event='event1'
所以从查询1和查询2中,我取出了teamid。
Table1:有55条记录
Table2:有5条记录。
我想要table1条teamid记录不应该出现在table2条teamid记录中。
所需的查询应 return 50 条记录。
我已经应用了 Left Join, NOT IN 但它不起作用,因为上述两个查询具有不同的 where 子句。
我会推荐 not exists
:
select *
from table1 t1
where event1pay = 'Paid' and not exists (
select 1
from table2 t2
where t2.event = 'event1' and t2.teamid = t1.teamid
)
如果你想用反left join
来做到这一点,那就是:
select t1.*
from table1 t1
left join table2 t2 on t2.teamid = t1.teamid and t2.event = 'event1'
where t1.event1pay = 'Paid' and t2.teamid is null
但我发现 not exists
是表达您想在这里做的事情的更好方式。