【问题标题】:Pull name/value from other table where id is the same从 id 相同的其他表中提取名称/值
【发布时间】:2013-02-06 00:23:48
【问题描述】:

分销商表

name          id
usf south     1
usf north     2
usf east      3
usf west      4

ogi 表

distributor    itemname
1              cheese
1              apples
3              oranges
2              pineapples

我目前在页面上的每个项目名称显示的“ogi 表”中的“分销商”列中找到了编号。但是,我想将此编号更改为正确的分销商名称,可在“分销商表”中找到。我该怎么做?

感谢您的帮助。

【问题讨论】:

  • +1 用于格式良好的示例。

标签: php mysql sql select


【解决方案1】:

如果您有兴趣只替换查询而不替换任何下划线编程,这可能是一个可行的解决方案:

SELECT
    ogi.itemname,
    distributors.name AS distributor
FROM
    ogi
INNER JOIN
    distributors
    ON
    ogi.distributor = distributors.id

【讨论】:

  • 这太完美了。不必修改我的任何 php 代码。感谢您的帮助!我认为这是一个内部连接,但我不太确定如何完成它(对 mysql 来说有点新)。再次感谢!
【解决方案2】:
SELECT  a.*, b.*
FROM    distributors a
        INNER JOIN ogi b
            ON a.id = b.distributor

要全面了解联接,请访问以下链接:

【讨论】:

    【解决方案3】:

    本教程将向您展示如何存档。

    http://www.tizag.com/mysqlTutorial/mysqljoins.php

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多