我是sql的新手,我真的试图自己解决这个问题,但没有成功...... 希望有人可以提供帮助。
我有3张桌子:
AUTH_USER
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(30) | NO | UNI | NULL | |
| first_name | varchar(30) | NO | | NULL | |
| last_name | varchar(30) | NO | | NULL | |
| email | varchar(75) | NO | | NULL | |
| password | varchar(128) | NO | | NULL | |
| is_staff | tinyint(1) | NO | | NULL | |
| is_active | tinyint(1) | NO | | NULL | |
| is_superuser | tinyint(1) | NO | | NULL | |
| last_login | datetime | NO | | NULL | |
| date_joined | datetime | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
catalog_presents
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(48) | NO | | NULL | |
| slug | varchar(50) | NO | UNI | NULL | |
| amount | smallint(5) unsigned | NO | | 0 | |
| points | smallint(5) unsigned | NO | | 500 | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| active | tinyint(1) | NO | | NULL | |
| image | varchar(100) | YES | | NULL | |
| description | longtext | YES | | NULL | |
+-------------+----------------------+------+-----+---------+----------------+
catalog_orders
+------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| status | smallint(5) unsigned | NO | | 0 | |
| present_id | int(11) | NO | MUL | NULL | |
| address_id | int(11) | NO | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+----------------------+------+-----+---------+----------------+
我正在尝试选择一个用户并检查他是否订购了一个特定的礼物并相应地写入该行,然后检查另一个,另一个,依此类推。
基本上我希望结果看起来像这样:
id present1 present2 present3 present4
1 1 0 0
2 1 1 1
3 0 0 0
4 1 0 1
其中1是用户在该类型的礼物上下订单而0是他没有。
我的查询看起来像这样
select auth_user.id, case when present_id = 1 from auth_user
left join catalog_orders on catalog_orders.user_id = auth_user.id
left join catalog_presents on catalog_presents.id = catalog_orders.present_id
问题在于,用户的所有不同订单都按多行排序:
id present1 present2 present3 present4
1 1 0 0
2 1 0 0
2 0 1 0
2 0 0 1
3 0 0 0
4 1 0 0
4 0 0 1
有人可以帮我解决这个问题吗?提前谢谢!
答案 0 :(得分:1)
这是一种数据透视表。
您走在正确的道路上,但您需要将结果合并为每个用户一行。
使用每个MAX()
的汇总auth_user.id
将它们折叠为一行。 CASE
为每个呈现提供零或一个,MAX()
然后为auth_user.id
选择所有行的最大值,如果现在是1
将是0
已经买了SELECT
auth_user.id,
MAX(CASE WHEN present_id = 1 THEN 1 ELSE 0 END) AS present1,
MAX(CASE WHEN present_id = 2 THEN 1 ELSE 0 END) AS present2,
MAX(CASE WHEN present_id = 3 THEN 1 ELSE 0 END) AS present3,
MAX(CASE WHEN present_id = 4 THEN 1 ELSE 0 END) AS present4
FROM
auth_user
LEFT JOIN catalog_orders on catalog_orders.user_id = auth_user.id
LEFT JOIN catalog_presents on catalog_presents.id = catalog_orders.present_id
GROUP BY auth_user.id
ORDER BY auth_user.id
。
CASE
特别是对于MySQL,您不需要present_id = 1
,因为布尔比较SELECT
auth_user.id,
/* MySQL ok, not all other RDBMS will do this - boolean comparison returns 1 or 0 */
MAX(present_id = 1) AS present1,
MAX(present_id = 2) AS present2,
MAX(present_id = 3) AS present3,
MAX(present_id = 4) AS present4
FROM
auth_user
LEFT JOIN catalog_orders on catalog_orders.user_id = auth_user.id
LEFT JOIN catalog_presents on catalog_presents.id = catalog_orders.present_id
GROUP BY auth_user.id
ORDER BY auth_user.id
将自己返回1或0。但是,这不适用于所有其他RDBMS。上述方法是首选。
{{1}}