我有 2 张 table :
- 客户
- 客户沟通
客户表具有唯一的 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/