[英]select from table where rows from another table do not match
I have 2 tables: 我有2张桌子:
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.