简体   繁体   English

如何在PHP上获取正确的SQL值

[英]How to get the correct sql value on php

How to get the correct sql value from this table on php 如何从PHP上的此表中获取正确的SQL值

I have two tables below; 我下面有两个表;

Table: A

StateID   StudentID   Attendee
---------------------------------
ITB001      10          John
ITB001      20          Bob
ITB001      40          Mickey
ITB001      60          Jenny
ITB001      30          James
ITB001      70          Erica

Table: B

StateID   StudentID    Attendee
---------------------------------
ITB001       10          John
ITB001       30          James

I want to select and output Attendee value from Table A where is minus Table B. If Attendee from Table B have value John and James if so it will list Attendee value from Table A and only output without John and James on Table A list. 我想从表A中减去表B并从中选择Attendee值并输出。如果表B中的与会者具有John和James值,那么它将从表A中列出Attendee值,并且仅在表A列表中没有John和James的情况下输出。 So final output will be: 因此最终输出将是:

StateID   StudentID   Attendee
---------------------------------
ITB001      20          Bob
ITB001      40          Mickey
ITB001      60          Jenny
ITB001      70          Erica

Any help and hints would be appreciated. 任何帮助和提示,将不胜感激。 Thanks. 谢谢。

您可以通过以下方式做到这一点:

Select * from A where StudentID  not in (select StudentID from B where 1=1)
SELECT *
FROM TableA A
WHERE NOT EXISTS (SELECT 1
                  FROM TableB
                  WHERE Attendee = A.Attendee)

If I understand correctly, you want everything from table A that isn't already in table B. That would be possible using a LEFT JOIN: 如果我理解正确,那么您希望表A中的所有内容都不在表B中。使用LEFT JOIN可以实现:

SELECT A.*
    FROM A
        LEFT JOIN B
            ON A.StudentID = b.StudentID
                AND A.StateID = b.StateID
    WHERE B.StudentID IS NULL;

The [outer] left join lets you query for a full record set from the left operand, and partial from the right operand. [外部]左联接使您可以从左侧操作数查询完整记录集,从右侧操作数查询部分记录集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM