MySQL选择各自偏好的不同用户
假设我有一个用户和优先表,以及两个之间的桥梁表user_preference
:
/* user table: */
+----------+--------------+
| Field | Type |
+----------+--------------+
| id | int |
| username | varchar(255) |
+----------+--------------+
/* preference table: */
+------------+--------------+
| Field | Type |
+------------+--------------+
| preference | varchar(255) |
+------------+--------------+
/* user_preference table: */
+-----------------+--------------+
| Field | Type |
+-----------------+--------------+
| user_id | int |
| preference_name | varchar(255) |
+-----------------+--------------+
例如,有3个偏好可供选择:“游泳”,“看电视”,“骑自行车” 。一个用户可以具有零或所有3个偏好的偏好,这反映在user_preference
表上。
现在,我想查询10个不同的用户,并且所有这些用户都包括在内,包括无效或mutiple首选项,如何为此构建选择说明?
到目前为止,我已经尝试了这样的事情:
SELECT u.*, p.preference_name
FROM user u
LEFT JOIN user_preference p ON p.user_id = u.id
LIMIT 10;
/* Result: */
id | username | preference_name
1 | user1 | swimming
1 | user1 | cycling
2 | user2 | null
3 | user3 | watching TV
... /* rest of the result */
您可以看到结果将返回重复user1
,并且不会有10个不同的用户。我知道不同的
和组成的
关键字,它不能解决问题,因为它只会返回对用户的单个偏好,而用户可以有多个偏好。
如何使用一个选择语句做到这一点?
Let's say I have a user and preference table, as well as a bridge table user_preference
between the two:
/* user table: */
+----------+--------------+
| Field | Type |
+----------+--------------+
| id | int |
| username | varchar(255) |
+----------+--------------+
/* preference table: */
+------------+--------------+
| Field | Type |
+------------+--------------+
| preference | varchar(255) |
+------------+--------------+
/* user_preference table: */
+-----------------+--------------+
| Field | Type |
+-----------------+--------------+
| user_id | int |
| preference_name | varchar(255) |
+-----------------+--------------+
For instance there are 3 preferences to choose from: "swimming", "watching TV", "cycling". And one user can have zero or all 3 of the preferences, which is reflected on the user_preference
table.
Now I want to query 10 different users, and with all of them each of their own preferences included, either null or mutiple preferences, how to construct a select statement for that?
So far I have tried something like this:
SELECT u.*, p.preference_name
FROM user u
LEFT JOIN user_preference p ON p.user_id = u.id
LIMIT 10;
/* Result: */
id | username | preference_name
1 | user1 | swimming
1 | user1 | cycling
2 | user2 | null
3 | user3 | watching TV
... /* rest of the result */
As you can see the result will return a duplicate user1
, and it won't be 10 distinct users. I'm aware of the distinct
and group by
keywords, it doesn't solve the problem, as it will only return a single preference for a user, while the user can have multiple preferences.
How to do that with one single select statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下。
group_concat()将为每个用户提供逗号分隔的首选项列表。
Pro Tip 。当桌子变得很大时,更改枚举以添加更多值会非常耗时。另外,设计数据库通常是不明智的,因此它需要大量的Alter表语句。因此,如果您希望自己的偏好是开放式的,那么您概述的方法是正确的方法。
Try this.
The GROUP_CONCAT() will make a comma-separated list of preferences for each user.
Pro tip. When tables get very large, altering ENUMs to add more values gets very time-consuming. Plus, it's usually unwise to design a database so it needs lots of ALTER TABLE statements as it grows. So, the approach you have outlined is the right way to go if you want your possible preferences to be open-ended.