【问题标题】:MySQL join, return 1 row from one table and multiple rows from another table as an array or listMySQL连接,从一个表返回1行,从另一个表返回多行作为数组或列表
【发布时间】:2017-03-11 20:58:42
【问题描述】:

我正在开展一个对笔记本电脑进行分类的项目,因此我正在尝试尽可能多地重复使用信息。 MySQL 表的简化版本是:

Table: laptop
|----------------------------------|
| id | make | line | model         |
| 1  | 1    | 1    | Late 2015 13" | 
|----------------------------------|

Table: make 
|----------------------------------|
| id | name  | other info          |
| 1  | Apple |                     |
|----------------------------------|

Table: line
|----------------------------------|
| id | name        | other info    |
| 1  | MacBook Pro |               |
|----------------------------------|

Table: networking
|----------------------------------|
| id | name         | other info   |
| 1  | A wifi card  |              |
| 2  | Another card |              |
| 3  | Yet another  |              |
|----------------------------------|

Table: laptop_networking 
|----------------------------------|
| id | networking | laptop         |
| 1  | 3          | 1              |
| 2  | 1          | 1              |
|----------------------------------|

到目前为止,我使用当前语句在 PHP 中检索数据

$statement = $dbc->prepare("
SELECT l.id
     , m.id AS makeID
     , m.name AS makeName
     , n.id AS lineID
     , n.name AS lineName
     , l.model
  FROM laptop l
  JOIN make m
    ON l.make = m.id
  JOIN line n
    ON l.line = n.id
 WHERE l.id = :laptop);
$statement->bindParam(':laptop', $anID, PDO::PARAM_INT);
$statement->execute();
$theLaptop = $statement0>fetch();

目前以 $anID = 1 运行此代码会返回

|---------------------------------------------------------------|
| id | makeID | makeName | lineID | lineName    | Model         |
| 1  | 1      | Apple    | 1      | MacBook Pro | Late 2015 13" | 
|---------------------------------------------------------------|

我想做的是将另一列附加到表中,该表返回来自 Networking 的所有名称,其 ID 等于laptop_networking 中的一行,其中笔记本电脑字段等于检索到的笔记本电脑行中的 ID

如:

|------------------------------------------------------------------------------------------|
| id | makeID | makeName | lineID | lineName    | model         | networking               |
| 1  | 1      | Apple    | 1      | MacBook Pro | Late 2015 13" | Yet another, A wifi card |
|------------------------------------------------------------------------------------------|

这可能吗,因为我在不同类型的 JOIN 上的多次尝试都没有产生预期的结果。

谢谢

【问题讨论】:

  • 查询字符串没有右引号

标签: php mysql join


【解决方案1】:

试试这个查询:

SELECT laptop.id,
       make.id AS makeID,
       make.name AS makeName,
       line.id AS lineID,
       line.name AS lineName,
       laptop.model,
       t.networking
FROM laptop
INNER JOIN make
    ON laptop.make = make.id
INNER JOIN line
    ON laptop.line = line.id
INNER JOIN
(
    SELECT t1.laptop, GROUP_CONCAT(t2.name) AS networking
    FROM laptop_networking t1
    INNER JOIN networking t2
        ON t1.networking = t2.id
    GROUP BY t1.laptop
) t
    ON laptop.id = t.laptop
WHERE laptop.id = :laptop

演示在这里:

Rextester

【讨论】:

  • 比较合适
  • 谢谢,我刚刚将上面列出的 INNER JOIN 添加到完整代码中,并在第一次测试中收到了预期的结果。
猜你喜欢
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 2012-08-15
  • 2011-05-13
  • 1970-01-01
  • 2010-10-27
  • 2012-03-22
  • 2019-03-18
相关资源
最近更新 更多