简体   繁体   English

内部联接2张桌子多个结果

[英]Inner Join 2 tables multiple result

i have 2 tables, one for the products and one for the sizes. 我有2张桌子,一张用于产品,一张用于尺寸。 they have a relation with fk and the problem is that when I'm using the inner join I cannot use the "group by" in order to don't have repeated results. 它们与fk有关系,问题是当我使用内部联接时,为了没有重复的结果,我不能使用“分组依据”。 this is the code: 这是代码:

SELECT
sneakers.sneaker_id,
sneakers.sneaker_name,
sneakers.gender,
sneakers.description,
sneakers.price,
sizes.size,
brand_names.brand_name
FROM sneakers
INNER JOIN sizes ON sneaker_fk = sneaker_id

if i try to use the GROUP BY sneaker_fk it will give me this response: 如果我尝试使用GROUP BY sneaker_fk ,它将给我以下响应:

Error
SQL query: Documentation


SELECT sneakers.sneaker_id,sneakers.sneaker_name, sneakers.gender, sneakers.description,
                                    sneakers.price, sizes.size, brand_names.brand_name FROM sneakers
                                    INNER JOIN sizes ON sneaker_fk = sneaker_id
                                    INNER JOIN brand_names ON brand_name_fk = brand_name_id
                                    GROUP BY sneaker_fk LIMIT 0, 30
MySQL said: Documentation

#1055 - Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sneakerstore.sizes.size' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

What am I doing wrong?? 我究竟做错了什么?? Do you have any better solution to display One item with all the related sizes without having multiple results? 您是否有更好的解决方案来显示具有所有相关尺寸的一项而不显示多个结果?

I hope you can help me as fast as possible! 希望您能尽快帮助我! thanks in advance 提前致谢

Use MySQL's Group_Concat() function 使用MySQL的Group_Concat()函数

The MySQL GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group. MySQL GROUP_CONCAT()函数从一个组返回一个字符串,该字符串具有串联的非NULL值。

Excerpt from Using GROUP_CONCAT with joined tables 摘自结合表使用GROUP_CONCAT

GROUP_CONCAT works with joins as well. GROUP_CONCAT也适用于联接。

Let's say we have a table courses: 假设我们有一个表课程:

  | id | name |
  +—-+—————+
  | 1 | Ruby 101 |
  +—-+—————+
  | 2 | TDD for Poets |
  +—-+—————+'

We also have a second table bookings: 我们还有第二张桌子预订:

  | id | course_id |
  +—-+———–+
  | 7 | 1 |
  +—-+———–+
  | 8 | 1 |
  +—-+———–+
  | 9 | 1 |
  +—-+———–+
  | 10 | 2 |
  +—-+———–+
  | 11 | 2 |
  +—-+———–+


SELECT courses.name, GROUP_CONCAT(bookings.id)
FROM bookings
INNER JOIN courses ON courses.id == bookings.course_id
GROUP BY bookings.course_id;

The result set looks like this: 结果集如下所示:

| courses.name | GROUP_CONCAT(bookings.id) |
+—————+—————————+
| Ruby 101 | 7,8,9 |
+—————+—————————+
| TDD for Poets | 10,11 |
+—————+—————————+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM