3

我是 sql 新手,我真的试图自己解决这个问题,但没有成功......希望有人能提供帮助。

我有 3 张桌子:

身份验证用户

+--------------+--------------+------+-----+---------+----------------+
| 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    |                |
+--------------+--------------+------+-----+---------+----------------+

目录礼物

+-------------+----------------------+------+-----+---------+----------------+
| 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    |                |
+-------------+----------------------+------+-----+---------+----------------+

目录订单

+------------+----------------------+------+-----+---------+----------------+
| 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

有人可以帮我解决这个问题吗?提前致谢!

4

1 回答 1

1

这是一种数据透视表。

您走在正确的道路上,但您需要将结果合并到每个用户的一行。

使用聚合MAX()perauth_user.id将它们折叠成一行。为每个礼物CASE提供一个 0 或 1 ,然后MAX()auth_user.id10

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

特别是对于 MySQL,您不需要,CASE因为布尔比较present_id = 1将自行返回 1 或 0。但是,这不能移植到所有其他 RDBMS。上述方法是优选的。

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
于 2012-11-17T20:42:47.050 回答