【问题标题】:selecting from 2 database table to display从 2 个数据库表中选择要显示
【发布时间】:2017-03-06 06:23:50
【问题描述】:

我有两张桌子:

    products
id  | name  |  brand  |  desc
 1   pencil   brand1     desc1
 2   paper    brand2     desc2
 3   books    brand1     desc3
 4   pen      brand2     desc4
 5   crayons  brand1     desc5

和:

    supplier
id  | name  |  brand
 1    sup1     brand1
 2    sup2     brand2

我将如何在supplier brand下显示与brand1相关的所有产品和信息

例如:如果供应商brand1想查看所有产品,他会看到这样的信息:

我期待这样的结果

    products
id  | name  |  brand  |  desc
 1   pencil   brand1     desc1
 3   books    brand1     desc3
 5   crayons  brand1     desc5

我需要加入查询吗?有更简单的方法吗?我只是希望有人能帮助我,我目前正在使用 php 和 phpmyadmin,任何形式的代码都可以,只要它会在供应商表下显示具体结果。

【问题讨论】:

  • SELECT * FROM table1 LEFT JOIN table2 on table1.id = table2.id 试试这个

标签: php mysql database phpmyadmin xampp


【解决方案1】:

一个简单的左连接就可以解决问题。 像这样的:

SELECT p.name, p.brand, p.desc, s.name  FROM products p
LEFT JOIN supliers s ON s.brand = p.brand

虽然我会使用两个表的 ID 列来链接数据。

【讨论】:

    【解决方案2】:

    你可以通过点赞来实现,

    SELECT p.id, p.name, p.brand, p.desc, s.name  FROM products p LEFT JOIN supliers s ON s.brand = p.brand
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 2015-06-21
      • 2021-02-18
      • 2014-02-26
      • 1970-01-01
      相关资源
      最近更新 更多