php - 从另一个表中的行不匹配的表中选择

标签 php mysql sql

我有 2 张 table :

  1. 客户
  2. 客户沟通

客户表具有唯一的 sequence 列,customer_communication 表具有与客户表中的序列列匹配的 customer_seq

customer_communication 表中的行有一个日期时间列,我使用这些查询从两个表中选择数据:

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>';

因此,它会从 customer 表中选择所有行,然后从 customer_communication 表中选择其中 customer_seq = 序列且距日期时间列 15 天的行。

如何显示 customer_communication 表中不存在的 customer 表中的所有行

例如,customer 中有序列 1,但 customer_communication 表的 customer_seq 列中不存在该序列,因此我想显示此

最佳答案

这可以通过基本的 SQL 来完成。我将让您将其集成到您的 php 中。

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

关于php - 从另一个表中的行不匹配的表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19951115/

相关文章:

php - 如何在php中使用ajax响应数据

php - 文档错误标签

javascript - 如何使用 PHP 和 JQuery AJAX 在 <div> 标签中检索和插入 MySQL 数据?

java - Hibernate Envers 修订日期起的时间戳

php - 如何在 PHP 中的不同 session 之间创建共享对象?

mysql - 对于包含超过 1 件商品的订单,最多只能选择 2 行

MySQL日期函数(假设May-10保存为2010-05-01)

c# - 如何在命令启动后更改 sql 超时

php - 如果数量表中不存在 Laravel 显示表产品

php - 使用 ZendFramework2 请求 Random.org API