简体   繁体   English

将多个列和行合并为一个条目

[英]Combine multiple columns and rows into a single entry

I have a relational database with one main table linked to other tables with foreign keys. 我有一个关系数据库,其中一个主表通过外键链接到其他表。 I am trying to write a reporting tool in PHP to grab all the data out but have gotten stck with one part of it. 我正在尝试使用PHP编写报告工具以获取所有数据,但其中一部分已被stck接受。

here is my query so far: 到目前为止,这是我的查询:

SELECT * 
FROM student s 
LEFT OUTER JOIN ep e ON s.sID = e.sID 
LEFT OUTER JOIN ntc n ON s.sID = n.sID 
LEFT OUTER JOIN pk p ON s.sID = p.sID 
LEFT OUTER JOIN roa r ON s.sID = r.sID 
WHERE s.sID = '$id'

ep, ntc and pk are all 1:1 with student so there are no problems. ep,ntc和pk与学生的比例均为1:1,所以没有问题。 But the roa table holds multiple records (spanning multiple rows) for each student. 但是roa表为每个学生保留了多个记录(跨越多行)。

roa table structure: roa表结构:

+----------+--------------+------+-----+-------------------+----------------+
| Field    | Type         | Null | Key | Default           | Extra          |
+----------+--------------+------+-----+-------------------+----------------+
| roaID    | int(11)      | NO   | PRI | NULL              | auto_increment |
| sID      | int(11)      | NO   | MUL | NULL              |                |
| roa      | varchar(255) | NO   |     | NULL              |                |
| roaStaff | varchar(50)  | NO   |     | NULL              |                |
| visible  | tinyint(1)   | NO   |     | 1                 |                |
+----------+--------------+------+-----+-------------------+----------------+

where a select * from roa where sID = 1 returns: 其中select * from roa where sID = 1返回:

+-------+-----+---------------+----------+---------+
| roaID | sID | roa           | roaStaff | visible |
+-------+-----+---------------+----------+---------+
|    41 | 1   | Description 1 | Staff 1  |       1 |
|    60 | 1   | Description 2 | Staff 2  |       1 |
+-------+-----+---------------+----------+---------+

what I am after my original query to achieve is a result something like: 在执行原始查询后,我得到的结果是:

<-student, etc data
..-------+-----+-----------------------------------------------+---------+
.. roaID | sID | roa                                           | visible |
..-------+-----+-----------------------------------------------+---------+
..    41 | 1   | Description 1 Staff 1, Description 2 Staff 2  |       1 |
...
...

so that both the roa.roa and the roa.roaStaff are all in one cell. 因此roa.roa和roa.roaStaff都在一个单元格中。

I have tried using GROUP_CONCAT but have only been able to get it to work for one column, not group multiple columns together. 我尝试使用GROUP_CONCAT但只能使它适用于一列,而不是将多个列组合在一起。

any help is greatly appreciated. 任何帮助是极大的赞赏。

regards. 问候。

try something like this, 尝试这样的事情,

SELECT ......, GROUP_CONCAT(CONCAT(roa, ' ', roaStaff)) roa
FROM ...

so when you plug it in your query, 因此,当您将其插入查询中时,

SELECT * 
FROM student s 
LEFT OUTER JOIN ep e ON s.sID = e.sID 
LEFT OUTER JOIN ntc n ON s.sID = n.sID 
LEFT OUTER JOIN pk p ON s.sID = p.sID 
LEFT OUTER JOIN 
(
    SELECT sID, GROUP_CONCAT(CONCAT(roa, ' ', roaStaff)) roa
    FROM roa 
    GROUP BY sID
) r ON s.sID = r.sID 
WHERE s.sID = '$id'

follow-up question, how about the visible field on table roa ? 后续问题,表roa上的visible字段如何? How would you want it to be shown 您希望如何显示它

SIDENOTE 边注

your query is vulnerable with SQL INJECTION , please read the article below to learn how to protect from it 您的查询容易受到SQL INJECTION攻击,请阅读下面的文章以了解如何保护它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM