Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表1记录不应出现在表2中_Php_Mysql_Sql_Subquery_Left Join - Fatal编程技术网

Php 表1记录不应出现在表2中

Php 表1记录不应出现在表2中,php,mysql,sql,subquery,left-join,Php,Mysql,Sql,Subquery,Left Join,我在mysql的同一个数据库中有两个表: 表1 表2 我正在使用phpmyadmin和php 我在两个表中都有列'teamid',表1包含几个不同的事件列。 因此,我从表1中抽出了参加特定活动的teamid 我在表2中还有列teamid,他们参加了特定的活动。因此,我还从表2中抽出了参加特定活动的teamid 查询1:从表1中选择teamid,其中event1pay='Pay' 查询2:从表2中选择teamid,其中event='event1' 因此,从查询1和查询2中,我提取了teamid 表

我在mysql的同一个数据库中有两个表:

  • 表1
  • 表2
  • 我正在使用phpmyadmin和php

    我在两个表中都有列'teamid',表1包含几个不同的事件列。 因此,我从表1中抽出了参加特定活动的teamid

    我在表2中还有列teamid,他们参加了特定的活动。因此,我还从表2中抽出了参加特定活动的teamid

    查询1:
    表1
    中选择teamid,其中event1pay='Pay'

    查询2:
    表2中选择teamid,其中event='event1'

    因此,从查询1和查询2中,我提取了teamid

    表1:它有55条记录

    表2:它有5条记录

    我希望table1 teamid记录不应出现在table2 teamid记录中

    所需查询应返回50条记录


    我已经应用了左连接,不在,但它不起作用,因为上述两个查询都有不同的where子句。

    我建议
    不存在

    select *
    from table1 t1
    where  event1pay = 'Paid' and not exists (
        select 1 
        from table2 t2 
        where t2.event = 'event1' and t2.teamid = t1.teamid
    )
    
    如果要使用反
    左连接执行此操作,则应:

    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
    

    但是我发现,
    不存在
    是一种更好的方式来表达你想在这里做什么。

    那么,你有什么问题?这里没有足够的代码来帮助您,或者它是如何让您失败的。您是否尝试了table1.teamid上的
    表2.teamid
    ?请显示您尝试的查询并解释其失败原因。两个表中都有列teamid。我希望table1 teamid记录不应出现在table2列teamid中。表1中有许多活动,我从表1中提取了teamid,他们参与了各自的活动并为其付费。表2列出了只参加过各自活动的团队。所以我只希望table1 teamid列记录不应该出现在table2 teamid列中,为什么不修改这个问题,使t1有10条记录,t2有2条记录,并且非常感谢!!这就是我一直在寻找的问题。我已经检查了数据库,得到了所需的输出。再次感谢你。我已经检查了not exists查询,它运行良好。@RahulSahu如果解决方案对您有效,那么您应该接受它作为答案,而不是留下评论。