我是 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
有人可以帮我解决这个问题吗?提前致谢!