mysql 组连接

发布于 2024-08-26 17:42:07 字数 1169 浏览 10 评论 0原文

我想列出所有用户及其相应的用户类别。 这是我的表的简化版本

CREATE TABLE users (
    user_id INT NOT NULL AUTO_INCREMENT,
    user_class VARCHAR(100),
    PRIMARY KEY (user_id)
);
INSERT INTO users VALUES
    (1, '1'),
    (2, '2'),
    (3, '1,2');
CREATE TABLE classes (
    class_id INT NOT NULL AUTO_INCREMENT,
    class_name VARCHAR(100),
    PRIMARY KEY (class_id)
);
INSERT INTO classes VALUES
    (1, 'Class 1'),
    (2, 'Class 2');

,这是我尝试使用的查询语句,但仅返回第一个匹配的用户类,而不是所希望的串联列表。

SELECT user_id, GROUP_CONCAT(DISTINCT class_name SEPARATOR ",") AS class_name
FROM users, classes
WHERE user_class IN (class_id)
GROUP BY user_id;

实际输出

+---------+------------+
| user_id | class_name |
+---------+------------+
|       1 | Class 1    |
|       2 | Class 2    |
|       3 | Class 1    |
+---------+------------+

想要的输出

+---------+---------------------+
| user_id | class_name          |
+---------+---------------------+
|       1 | Class 1             |
|       2 | Class 2             |
|       3 | Class 1, Class 2    |
+---------+---------------------+

提前致谢

I want to list all users with their corropsonding user class.
Here are simplified versions of my tables

CREATE TABLE users (
    user_id INT NOT NULL AUTO_INCREMENT,
    user_class VARCHAR(100),
    PRIMARY KEY (user_id)
);
INSERT INTO users VALUES
    (1, '1'),
    (2, '2'),
    (3, '1,2');
CREATE TABLE classes (
    class_id INT NOT NULL AUTO_INCREMENT,
    class_name VARCHAR(100),
    PRIMARY KEY (class_id)
);
INSERT INTO classes VALUES
    (1, 'Class 1'),
    (2, 'Class 2');

And this is the query statement I am trying to use but is only returning the first matching user class and not a concatenated list as hoped.

SELECT user_id, GROUP_CONCAT(DISTINCT class_name SEPARATOR ",") AS class_name
FROM users, classes
WHERE user_class IN (class_id)
GROUP BY user_id;

Actual Output

+---------+------------+
| user_id | class_name |
+---------+------------+
|       1 | Class 1    |
|       2 | Class 2    |
|       3 | Class 1    |
+---------+------------+

Wanted Output

+---------+---------------------+
| user_id | class_name          |
+---------+---------------------+
|       1 | Class 1             |
|       2 | Class 2             |
|       3 | Class 1, Class 2    |
+---------+---------------------+

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总攻大人 2024-09-02 17:42:07

这不是最好的设计,但这是您想要的查询:

SELECT  user_id, GROUP_CONCAT(class_name)
FROM    users
JOIN    classes
ON      FIND_IN_SET(class_id, user_class)
GROUP BY
        user_id

This is not the best design, but here's the query you want:

SELECT  user_id, GROUP_CONCAT(class_name)
FROM    users
JOIN    classes
ON      FIND_IN_SET(class_id, user_class)
GROUP BY
        user_id
奢华的一滴泪 2024-09-02 17:42:07

这违反了数据库规范化的规则。不要将多对多值存储在以逗号分隔的列表中。相反,创建另一个表 user_classes,其中包含字段 user_id 和 class_id。该表只是用作其他两个类之间的链接表。之后你可以使用 GROUP_CONCAT 来做你想做的事情。

This breaks rules of database normalization. Don't store many-to-many values in a comma-separated list. Instead make another table user_classes with fields user_id and class_id. This table is simply used as a link table between the other two classes. After this you can use GROUP_CONCAT to do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文