Mysql 从第三个表中获取记录

Mysql 从第三个表中获取记录,mysql,join,group-by,group-concat,Mysql,Join,Group By,Group Concat,我有三个表,还有重复的列名:)我想将相册连接到产品,将图像连接到相册。图像很多。尝试这样的查询,它会给我重复的产品。是否有机会在一个查询中获取所有信息 SELECT *, p.name as nazwa, a.name as nazwa_al, i.name as obrazek FROM products p JOIN albums a on p.album_id=a.id JOIN

我有三个表,还有重复的列名:)我想将相册连接到产品,将图像连接到相册。图像很多。尝试这样的查询,它会给我重复的产品。是否有机会在一个查询中获取所有信息

        SELECT
        *, p.name as nazwa, a.name as nazwa_al, i.name as obrazek
        FROM products p
        JOIN 
        albums a on p.album_id=a.id
        JOIN
          (SELECT *, images.name AS nazwa_im FROM images ORDER BY images.order ASC) i
        ON i.album_id=a.id
        ORDER BY p.order ASC
产品

+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| name        | text    | NO   |     | NULL    |                |
| description | text    | NO   |     | NULL    |                |
| album_id    | int(11) | YES  |     | NULL    |                |
| order       | int(11) | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| name  | text    | NO   |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| name     | text    | NO   |     | NULL    |                |
| alt      | text    | NO   |     | NULL    |                |
| album_id | int(11) | NO   |     | NULL    |                |
| order    | int(11) | NO   |     | NULL    |                |
+----------+---------+------+-----+---------+----------------+
专辑

+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| name        | text    | NO   |     | NULL    |                |
| description | text    | NO   |     | NULL    |                |
| album_id    | int(11) | YES  |     | NULL    |                |
| order       | int(11) | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| name  | text    | NO   |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| name     | text    | NO   |     | NULL    |                |
| alt      | text    | NO   |     | NULL    |                |
| album_id | int(11) | NO   |     | NULL    |                |
| order    | int(11) | NO   |     | NULL    |                |
+----------+---------+------+-----+---------+----------------+
图像

+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| name        | text    | NO   |     | NULL    |                |
| description | text    | NO   |     | NULL    |                |
| album_id    | int(11) | YES  |     | NULL    |                |
| order       | int(11) | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| name  | text    | NO   |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| name     | text    | NO   |     | NULL    |                |
| alt      | text    | NO   |     | NULL    |                |
| album_id | int(11) | NO   |     | NULL    |                |
| order    | int(11) | NO   |     | NULL    |                |
+----------+---------+------+-----+---------+----------------+

为了简单起见,我不想修改db的结构。对我来说,最简单的解决方案是:一个产品=>一个相册=>多个图像

使用连接和别名来解决重复名称错误

您可以使用distint或group by,使结果按照相同的产品id对齐

SELECT
*, p.name as nazwa, a.name as nazwa_al, i.name as obrazek
FROM 
products p
JOIN 
albums a on p.album_id = a.id
JOIN
images i ON i.album_id = a.id
GROUP BY p.id
ORDER BY p.order ASC
如果右侧有多行,则需要使用
分组\u concat

SELECT
*, p.name as nazwa, a.name as nazwa_al, group_concat(i.name) as obrazek
FROM 
products p
JOIN 
albums a on p.album_id = a.id
JOIN
images i ON i.album_id = a.id
GROUP BY p.id
ORDER BY p.order ASC

非常感谢。它给了我语法错误,我必须在order by之前移动group by,现在它也只给了我一个图像“obrazek”-这就是I.name。未测试的查询。更新的查询。有帮助吗?还是需要帮助?@cssBlaster21895:检查我的最新答案?让我知道它是否有效?同一列上不能有两个
as
别名。所以要么
作为条形码
,要么
作为obrazek
,但不能两者兼而有之。@Barmar:copy-paste打字:(.Sorry.Updated-response。现在有任何错误吗?