【问题标题】:PHP MySQL join 3 tables and output result [duplicate]PHP MySQL连接3个表并输出结果[重复]
【发布时间】:2014-08-08 13:08:50
【问题描述】:

我有如下三张表:

项目:

-----------------------------------------------------
|  itemID  |  itemName  |  categoryID  |  sellerID  |
-----------------------------------------------------
|  1       |  item1     |  c1          |  s1        |
|  2       |  item2     |  c1          |  s2        |
|  3       |  item3     |  c3          |  s2        |
|  4       |  item4     |  c2          |  s3        |
-----------------------------------------------------

分类:

---------------------------------
|  categoryID  |  categoryName  |
---------------------------------
|  c1          |  category1     |
|  c2          |  category2     |
|  c3          |  category3     |
---------------------------------

卖家:

-----------------------------
|  sellerID  |  sellerName  |
-----------------------------
|  s1        |  seller1     |
|  s2        |  seller2     |
|  s3        |  seller3     |
-----------------------------

我想从 Items 表中选择 categoryID 为 c1 的项目并显示如下:

----------------------------------------------
|  itemName  |  categoryName  |  sellerName  |
----------------------------------------------
|  item1     |  category1     |  seller1     |
|  item2     |  category1     |  seller2     |
----------------------------------------------

我不知道该怎么做。请帮帮我。

【问题讨论】:

    标签: php mysql sql database select


    【解决方案1】:

    看看JOIN:

    SELECT i.itemName, c.categoryName, s.sellerName
    FROM Items i
    JOIN Categories c
      ON c.id = i.categoryID
    JOIN Seller s
      ON s.id = i.sellerID
    WHERE i.categoryID = 'c1'
    

    【讨论】:

    • 成功了。非常感谢。我写了几乎相同的查询。唯一的区别是我使用的是INNER JOIN 而不是JOIN。我想我必须彻底研究它并了解所有不同类型的连接之间的基本区别。再次感谢。
    • 不客气,查看this关于加入差异的问题。
    【解决方案2】:

    类似这样的:

    select items.itemName, categories.categoryName, seller.sellerName
    from items
    join categories on items.categoryId = categories.categoryId
    join seller on items.sellerId = seller.sellerId
    where categories.categoryId = ?
    

    【讨论】:

      【解决方案3】:
      SELECT itemName, categories.categoryName, seller.sellerName 
      FROM items 
      INNER JOIN categories ON items.categoryID = categories.categoryID 
      INNER JOIN seller ON items.sellerID = seller.sellerID 
      WHERE items.categoryID = 'c1' 
      

      【讨论】:

        【解决方案4】:

        试试这个:

        $strSql = 'SELECT a.`itemName`,b.`categoryName`,c.`sellerName` FROM `Items` a INNER JOIN `Categories` b ON a.`categoryID` = b.`categoryID`
                 INNER JOIN `Seller` c ON a.`sellerID` = c.`sellerID` WHERE a.`categoryID` = "c1" ';
        

        【讨论】:

          【解决方案5】:

          这只是创建正确连接的问题。在这种情况下,您需要将表格附加到两个不同的东西上。语法应如下所示:

          $stmt = 'SELECT a.itemName,b.categoryName,c.sellerName FROM `[itemsTable]`
              inner join a  on b.categoryID  = a.categoryID  
          inner join a on c.sellerID = a.sellerID';
          

          【讨论】:

            猜你喜欢
            • 2011-11-24
            • 2014-05-25
            • 1970-01-01
            • 2014-02-04
            • 2019-04-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-11-25
            相关资源
            最近更新 更多