Select子查询错误子查询returns多于1行
Select subquery error Subquery returns more than 1 row
Table 1
+------------+--------------------+
| Serialnum | Products |
+------------+--------------------+
| 1 | Earbuds |
| 2 | Phone |
| 3 | Charger |
| 4 | Data Cable |
| 5 | Speakers |
+------------+--------------------+
Table 2
+------------+--------------------+
| product_id | Brands |
+------------+--------------------+
| 1 | Samsung |
| 1 | Xaomi |
| 1 | JBL |
| 2 | Ronin |
| 2 | Apple |
+------------+--------------------+
SELECT products.Serialnum,
products.Product_Name,
(SELECT GROUP_CONCAT(brand_name)
FROM products_brands
GROUP BY product_id)
FROM `products`
INNER JOIN products_brands ON products.Serialnum=products_brands.product_id
#1242 - 子查询 returns 多于 1 行
我正在尝试从另一个 table 获取串联值,并从我在外键中给出的此 table 匹配 ID
像这样:
+------------+------------------------------+
| Products | Brands |
+------------+------------------------------+
|Earbuds |Samsung,Xaomi,JBL,Ronin,Apple |
|Phone | Xaomi,Samsung,Apple |
|Data Cable | Ronin,Apple,Samsung |
|Speakers | JBL |
|Charger | Ronin,Apple,Samsung |
+------------+------------------------------+
编辑:
我更改了代码:
SELECT products.Serialnum,products.Product_Name FROM `products`
INNER JOIN (SELECT GROUP_CONCAT(brand_name)
FROM products_brands
GROUP BY product_id) products_brands
ON products.Serialnum=products_brands.product_id
新错误:#1054 - 'on clause'
中的未知列 'products_brands.product_id'
我认为你根据你的数据得出的预期结果是错误的。
CREATE TABLE products(
Serialnum int(9),
Products VARCHAR(50)
);
insert into products values (1,'Earbuds'),
(2,'Phone'),
(3,'Charger'),
(4,'Data Cable'),
(5,'Speakers');
CREATE TABLE products_brands(
product_id int(9),
Brands VARCHAR(50)
);
insert into products_brands values (1,'Samsung'),
(1,'Xaomi'),
(1,'JBL'),
(1,'Ronin'),
(1,'Apple');
正确的查询应该是:
SELECT products.Products,
GROUP_CONCAT(Brands) as Brands
FROM products
INNER JOIN products_brands ON products.Serialnum=products_brands.product_id
GROUP BY Products;
Table 1
+------------+--------------------+
| Serialnum | Products |
+------------+--------------------+
| 1 | Earbuds |
| 2 | Phone |
| 3 | Charger |
| 4 | Data Cable |
| 5 | Speakers |
+------------+--------------------+
Table 2
+------------+--------------------+
| product_id | Brands |
+------------+--------------------+
| 1 | Samsung |
| 1 | Xaomi |
| 1 | JBL |
| 2 | Ronin |
| 2 | Apple |
+------------+--------------------+
SELECT products.Serialnum,
products.Product_Name,
(SELECT GROUP_CONCAT(brand_name)
FROM products_brands
GROUP BY product_id)
FROM `products`
INNER JOIN products_brands ON products.Serialnum=products_brands.product_id
#1242 - 子查询 returns 多于 1 行
我正在尝试从另一个 table 获取串联值,并从我在外键中给出的此 table 匹配 ID 像这样:
+------------+------------------------------+
| Products | Brands |
+------------+------------------------------+
|Earbuds |Samsung,Xaomi,JBL,Ronin,Apple |
|Phone | Xaomi,Samsung,Apple |
|Data Cable | Ronin,Apple,Samsung |
|Speakers | JBL |
|Charger | Ronin,Apple,Samsung |
+------------+------------------------------+
编辑: 我更改了代码:
SELECT products.Serialnum,products.Product_Name FROM `products`
INNER JOIN (SELECT GROUP_CONCAT(brand_name)
FROM products_brands
GROUP BY product_id) products_brands
ON products.Serialnum=products_brands.product_id
新错误:#1054 - 'on clause'
中的未知列 'products_brands.product_id'我认为你根据你的数据得出的预期结果是错误的。
CREATE TABLE products(
Serialnum int(9),
Products VARCHAR(50)
);
insert into products values (1,'Earbuds'),
(2,'Phone'),
(3,'Charger'),
(4,'Data Cable'),
(5,'Speakers');
CREATE TABLE products_brands(
product_id int(9),
Brands VARCHAR(50)
);
insert into products_brands values (1,'Samsung'),
(1,'Xaomi'),
(1,'JBL'),
(1,'Ronin'),
(1,'Apple');
正确的查询应该是:
SELECT products.Products,
GROUP_CONCAT(Brands) as Brands
FROM products
INNER JOIN products_brands ON products.Serialnum=products_brands.product_id
GROUP BY Products;