也许有点奇怪的标题描述,但我基本上想实现该GROUP_CONCAT()
功能所做的事情,然后才保留双重条目。
我有四个要加入的表client
,, doctor
,physio
和records
根据$client
我想获取客户详细信息的变量,主治医生和治疗师(三个表中的一行)并加入该用户的所有记录。
说在这种情况下$client = 1
。该records
表有五个记录所在的列r_client_id = 1
。如果我运行如下查询,我只会从records
表中获取一条记录,即第一次出现 where r_client_id = 1
(这当然是有道理的):
SELECT
client.c_id, client.c_name
doctor.d_name,
physio.p_name,
records.r_record
FROM
adm_clients AS client
INNER JOIN
norm_client_doctor AS ncd ON ncd.ncd_client_id = client.c_id
INNER JOIN
adm_doctor AS doctor ON doctor.d_id = ncd.ncd_doctor_id
INNER JOIN
norm_client_physio AS ncp ON ncp.ncp_client_id = client.c_id
INNER JOIN
adm_physio AS physio ON physio.p_id = ncp.ncp_physio_id
LEFT JOIN
adm_doctor_records AS records ON records.r_client_id = client.c_id
WHERE
client.c_id = '".$client."'
现在假设五个记录r_client_id = 1
是这样的:
+------+-------------+-------------------+----------+
| r_id | r_client_id | r_record | r_date |
+------+-------------+-------------------+----------+
| 1 | 1 | regular visit | 10/10/12 |
+------+-------------+-------------------+----------+
| 3 | 1 | emergency control | 24/10/12 |
+------+-------------+-------------------+----------+
| 7 | 1 | regular visit | 08/09/12 |
+------+-------------+-------------------+----------+
| 18 | 1 | delivery | 03/01/12 |
+------+-------------+-------------------+----------+
| 20 | 1 | health checkup | 10/12/11 |
+------+-------------+-------------------+----------+
我希望我的输出是这样的数组:
Client 1
- Name Doctor
- Name Physio
Records
- Emergency control, 24/10/12
- Regular visit, 10/10/12
- Regular visit, 08/09/12
- Delivery, 03/01/12
- Health checkup, 10/12/11
我能想到的最接近的一个是GROUP_CONCAT()
在记录上添加一个,但是,当然,对“定期访问”进行分组,所以我会得到 4 行而不是 5
GROUP_CONCAT(DISTINCT records.r_record SEPARATOR '|')
[..]
echo(str_replace("|","<br>",$show->r_record));
有人知道如何显示所有匹配的记录吗?我觉得我很接近了,但我现在别无选择..
编辑:
我忘了提到当我删除时DISTINCT
,它会显示所有记录两次..
解决了:
让它像这样工作:
GROUP_CONCAT(DISTINCT
CONCAT (records.r_date, '~', records.r_record, '~', records.r_paraph)
SEPARATOR '|') AS clientDoctorRecords,