dongren2128 2016-03-16 17:38
浏览 31
已采纳

PHP + mysql - 左连接 - 三个表

I've got a weird question. I have three tables: - table_a with columns (id, product_id, customer_id, location, phone) - table_b with columns (id, product_id, product_name) - table_c with columns (id, customer_id, customer_name)

What I would like to display is a table with following content: table_a.id, product_name, customer_name, location, phone

So I assume somehow I should use left join table_b, table_c with table_a.

I've tried multiple approaches like:

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

I want to avoid making duplicates of product_id/customer_id in final table..

  • 写回答

3条回答 默认 最新

  • doufu5521 2016-03-16 17:44
    关注

    You can get all the other fields, but which id are you trying to get? All 3 tables have id field.

    Assuming you can take the id of first table:

    SELECT table_a.id, table_b.product_name as product_name, table_c.customer_name as customer_name, table_a.location as location, table_a.phone as 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
    

    Hope this helps.

    Peace! xD

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题