简体   繁体   English

从表中选择其他表中的行不匹配的表

[英]select from table where rows from another table do not match

I have 2 tables: 我有2张桌子:

  1. customer 顾客
  2. customer_communication 客户沟通

the customer table has a unique sequence column and the customer_communication table has customer_seq which matches the sequence column in the customer table. 客户表具有唯一的sequence列,而客户通信表具有与客户表中的序列列匹配的customer_seq序列。

The rows in the customer_communication table have a datetime column, i am selecting data from both tables using these queries: customer_communication表中的行具有datetime列,我正在使用以下查询从两个表中选择数据:

echo '<table width="100%" border="0" cellspacing="10" cellpadding="10">';
    //select from the customer_communication table
    $sql="SELECT * from customer where company_status = '' and no_communication = '' order by company ASC ";
    $rs=mysql_query($sql,$conn);
    while($result=mysql_fetch_array($rs))
    {
        $sql2="SELECT * from customer_communication WHERE customer_seq = '".$result["sequence"]."' and datetime > DATE_ADD(DATE(now()), INTERVAL 15 DAY) order by datetime ASC ";
        $rs2=mysql_query($sql2,$conn);
        if(mysql_num_rows($rs2) > 0)
        {
            echo '<tr>
            <td><a href="customer_communication.php?seq='.$result["sequence"].'">'.$result["company"].'</a></td>
            </tr>';
        }
    }
    echo '</table>';

so it selects all the rows from the customer table, then selects the rows from the customer_communication table where the customer_seq = sequence and its been 15 days from the datetime column. 因此,它从“客户”表中选择所有行,然后从“ customer_communication”表中选择“ customer_seq = sequence”,并且距datetime列已有15天。

how can i show all the rows from the customer table that do not exist in the customer_communication table 如何显示客户表中customer_communication表中不存在的所有行

for example, there is sequence 1 in customer and this does not exist in the customer_seq column in the customer_communication table so i want to show this 例如,在customer中有序列1,而customer_communication表的customer_seq列中不存在序列1,因此我想显示此序列

That can be done by basic SQL. 这可以通过基本的SQL完成。 I'll leave it to you to integrate it into your php. 我将它留给您,以将其集成到您的php中。

SELECT * FROM Customer c
WHERE NOT EXISTS
    (SELECT * FROM Customer_Communication
    WHERE Customer_seq = c.Customer_Seq);

你可以试试这个吗

$sql="SELECT * FROM customer as a, customer_communication as b  WHERE a.company_status = '' AND a.no_communication = '' AND b.customer_seq NOT IN ( SELECT sequence FROM customer ) AND b.datetime > DATE_ADD(DATE(now()), INTERVAL 15 DAY) ORDER BY a.company ASC ";

There are multiple different ways to do this, but one way is to use a subquery to get the unique customer_seq values from customer_communication table, and then retrieve all rows from the customer table that don't have those values for sequence column. 有多种不同的方法可以执行此操作,但是一种方法是使用子查询从customer_communication表中获取唯一的customer_seq值,然后从customer表中检索所有不包含序列列值的行。

Select * from customer c
where c.sequence not in 
(select distinct customer_seq from customer_communication)

暂无
暂无

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

相关问题 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 从另一个ID不匹配的表中选择数据 - Select data from another table where IDs donot match 从一个表中选择计数,另一个表中有一个位置 - Select count from a table with a Where on another table 如何从表中选择所有行? - How do I SELECT all rows WHERE from a table? 从一个表中选择与另一表中的值匹配的不同行和随机行 - Select distinct and random rows from one table that match a value from another table Select from table where 子句 from array from another table - Select from table where clause from array from another table Mysql查询选择其中一个表中的总和与另一个表中的行总和进行比较 - Mysql Query select where the sum from one table is compared to the sum of rows in another table 从存在ID(从另一表)的一个表中选择 - Select from one table where id (from another table) exists 从一个表中选择行,但语句条件在另一个表中 - Pick rows from one table but where statement condition is in another table 从一个表中选择行,其中基于第二个表ID的另一个第二个表中的ID显示结果的顶部以及下一个结果,因为它是+ Mysql - Select rows from a table where id in another second table based on second table ids shows results top as well as next results as it is + Mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM