我有三个用于存储一些用户数据的表。
sub_domains
(体验子域在这里。匹配exp_id
和匹配。所以如果用户有多个子域,则and可以重复)id
exp
domain_id
id
misc
user_id
exp_id
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(50) | YES | | NULL | |
| domain_id | int(10) | NO | | NULL | |
| exp_id | int(50) | YES | | NULL | |
+-----------+---------+------+-----+---------+----------------+
misc
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | YES | | NULL | |
| category | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
exp
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | | NULL | |
| job_type | varchar(60) | YES | | NULL | |
| experience | varchar(500) | YES | | NULL | |
| exp_title | varchar(255) | YES | | NULL | |
| domain | varchar(125) | YES | | NULL | |
| start | varchar(20) | YES | | NULL | |
| end | varchar(20) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
这是我从 exp 获取数据的 atm 查询:
$query_str = "SELECT a.id, a.user_id, a.job_type, a.experience, a.exp_title, a.domain, a.start, a.end, b.name
FROM exp a
JOIN misc b ON a.domain=b.id
WHERE a.user_id = ?";
我已经从中获取了域名,misc
但我也想从中获取每个子域的名称,misc
但我不知道如何更改查询。
编辑
我对此进行了一些研究,并提出了以下建议:
$query_str = "SELECT a.id, a.user_id, a.job_type, a.experience, a.exp_title, a.domain, a.start, a.end, b.name,
(SELECT GROUP_CONCAT(domain_id) FROM sub_domains sd WHERE a.id=sd.exp_id) AS subs,
(SELECT GROUP_CONCAT(name) FROM misc WHERE id IN( subs ) ) AS subcats
FROM exp a
JOIN misc b ON a.domain=b.id
WHERE a.user_id = ?";
它几乎可以工作,子猫的 var_dump 只返回表中的第name
一个misc
。