在mysql中,同一个数据库中有两个表。
我使用phpmyadmin和php。
我在两个表中都有'teamid‘列,而表1包含了几个不同的事件列。因此,我已经从table1撤出了参与特定活动的茶。
我在table2也有专栏文章,他们参加了一些特别的活动。因此,我也从table2撤出了参加特定活动的茶。
查询1:从table1 中选择teamid,其中Select 1Pay=‘Paid’
查询2:从table2 中选择teamid,其中事件=‘event 1’
因此,从查询1和查询2中,我取出了teamid。
Table1:它有55张唱片
Table2:它有5张唱片。
我希望table1的茶氨记录不应该出现在table2的茶氨记录中。
所需的查询应该返回50条记录。
我已经应用了左联接,而不是IN,但它没有工作,因为上面的两个查询都有不同的where子句。
发布于 2020-10-12 13:36:56
我推荐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是一个更好的方式来表达你想在这里做什么。
https://stackoverflow.com/questions/64318872
复制相似问题