Php 合并3个不同MySQL表的值,联接表1和表2,并检查此联接中是否存在表3值

Php 合并3个不同MySQL表的值,联接表1和表2,并检查此联接中是否存在表3值,php,mysql,sql,Php,Mysql,Sql,我有3个MySQL表来检索它们的数据 表1包含以下行: id | name | total_amount | product_id | date --------------------------------------------- 1 | some name | some amount | some pid | some date 2 | some name1 | some amount1 | some pid1 | some date1 3 | some na

我有3个MySQL表来检索它们的数据

表1包含以下行:

id | name       | total_amount | product_id | date
---------------------------------------------
1  | some name  | some amount  | some pid   | some date
2  | some name1 | some amount1 | some pid1  | some date1
3  | some name2 | some amount2 | some pid2  | some date2
4  | some name3 | some amount3 | some pid3  | some date3
表2:

product_id | product_name
-------------------
some pid   | some product name
some pid1  | some product name1
some pid2  | some product name2
some pid3  | some product name3
表3:

id | total_amount  | product_id
-------------------------------
1  | some amount   | some pid 
2  | some amount2  | some pid2 
我使用表1和表2之间的内部联接及其产品id来列出如下值:

name       | total_amount | product_id | product_name        | date
--------------------------------------------------
some name  | some amount  | some pid   | some product name   | some date
some name1 | some amount1 | some pid1  | some product name1  | some date1
some name2 | some amount2 | some pid2  | some product name2  | some date2
some name3 | some amount3 | some pid3  | some product name3  | some date3
但是,如果表3的total_amount和product_id与表1的total_amount和product_id的值相同,我不想在联接表列表中显示行

所以我希望我的输出像:

name       | total_amount | product_id | product_name        | date
--------------------------------------------------
some name1 | some amount1 | some pid1  | some product name1  | some date1
some name3 | some amount3 | some pid3  | some product name3  | some date3

是否可以使用SQL查询或我应该尝试使用客户端语言PHP执行此操作?

Yu可以使用以下查询:

SELECT t1.*, t2.*
FROM table1 AS t1
JOIN table2 AS t2 ON t1.product_id = t2.product_id 
LEFT JOIN table3 AS t3 ON t3.product_id = t2.product_id AND t1.total_amount = t3.total_amount
WHERE t3.product_id IS NULL

上述查询将
table1
table2
字段
product\u id
连接起来,并在
table3
中有匹配的记录时过滤掉记录,这些记录具有相同的
product\u id
total\u amount
值。

直接的方法是使用
notexists
NOT in
:

select t1.name, t1.total_amount, t1.product_id, t2.product_name, t1.date
from t1
join t2 on t2.product_id = t1.product_id
where (t1.total_amount, t1.product_id) not in
(
  select t3.total_amount, t3.product_id
  from t3
);

加入查询的
在哪里?应为表3。联接列为空