PHP + mysql - 左连接 - 三张表

标签 php mysql left-join

我有一个奇怪的问题。我有三个表: - table_a 列(id,product_id,customer_id,位置,电话) - 带有列(id、product_id、product_name)的 table_b - 包含列(id、customer_id、customer_name)的 table_c

我想显示的是一个包含以下内容的表格: table_a.id, product_name, customer_name, location, phone

所以我假设我应该以某种方式使用左连接 table_b、table_c 和 table_a。

我尝试了多种方法,例如:

SELECT*FROM table_a INNER JOIN table_b, table_c
ON table_a.product_id = table_b.product_id
ON table_a.customer_id = table_c.customer_id

我想避免在最终表中复制 product_id/customer_id..

最佳答案

您需要依次连接 table_a 和 table_b 以获得 product_name,然后连接 table_a 和 table_c 以获得 customer_name。你可以试试这个:

SELECT table_a.id, product_name, customer_name, location, phone
FROM table_a 
    INNER JOIN table_b ON table_a.product_id = table_b.product_id
    INNER JOIN table_c ON table_a.customer_id = table_c.customer_id

关于PHP + mysql - 左连接 - 三张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36043021/

相关文章:

php - 如何在 php 数组中插入多个 html 输入字段数据?

mysql - Select Projections.constructor 中的子查询

java - SHA1 不会为相同的字符串输入生成相同的哈希值?

php - 如何在我的旧 PHP 非集中站点上集成 Zend Framework?

PHP 时间未按实际时间格式排序

javascript - .htaccess 重定向不会发生

mysql - 从一个带有连接的表中删除

php - 查询中的 COUNT() Likes 返回意外结果

sql - 将SQL语句转换为Linq语句(3表左连接+where)

Mysql,左加入只有第一个连接的记录 - 按日期