I have 2 tables:
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.
The rows in the customer_communication table have a datetime column, i am selecting data from both tables using these queries:
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.
how can i show all the rows from the customer table that do not exist in the customer_communication table
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
That can be done by basic SQL. I'll leave it to you to integrate it into your php.
SELECT * FROM Customer c
WHERE NOT EXISTS
(SELECT * FROM Customer_Communication
WHERE Customer_seq = c.Customer_Seq);
Can you try this,
$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.
Select * from customer c
where c.sequence not in
(select distinct customer_seq from customer_communication)