【发布时间】:2018-06-25 22:40:50
【问题描述】:
美好的一天。我有多对多的关系设置。我想知道如何将具有相同 ID 的行分组并将其反映为与条目上的每个链接连接。
// from this // to this but with each link
----------Classes---------- ----------Classes----------
| Student Id | Lecture ID | | Student Id | Lecture ID |
| 1 | 1 | | 1 | 1; 2; 3 |
| 1 | 2 | | 1 | 4; 2 |
| 1 | 3 | ---------------------------
| 2 | 4 |
| 2 | 5 |
---------------------------
这是查询(没有组连接):
$classes = "SELECT * FROM students, lectures, classes
WHERE students.student_id=classes.student_id
AND lectures.lecture_id=classes.lecture_id"
$class_result = mysqli_query($con, $classes);
while($class = mysqli_fetch_array($class_result){
echo '<table><tr>';
echo '<td><a href="student.php?id='.$class['student_id'].'">'.$class['student'].'</a></td>';
echo '<td><a href="lecture.php?id='.$class['lecture_id'].'">'.$class['lecture'].'</a></td>';
echo '</tr></table>';
}
----------Classes----------
| Student Id | Lecture ID |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
| 2 | 5 |
---------------------------
这是查询(带有组连接):
$classes = "SELECT students.student_id, student, lectures.lecture_id, GROUP_CONCAT(lecture SEPARATOR '; ')
FROM students, lectures, classes
WHERE students.student_id=classes.student_id
AND lectures.lecture_id=classes.lecture_id
GROUP BY student"
$class_result = mysqli_query($con, $classes);
while($class = mysqli_fetch_array($class_result){
echo '<table><tr>';
echo '<td><a href="student.php?id='.$class['student_id'].'">'.$class['student'].'</a></td>';
echo '<td><a href="lecture.php?id='.$class['lecture_id'].'">'.$class["GROUP_CONCAT(lecture SEPARATOR '; ')"].'</a></td>';
echo '</tr></table>';
}
----------Classes----------
| Student Id | Lecture ID | // this works but it cannot reflect each link of id of lecture
| 1 | 1; 2; 3 | // it only reflects link of one id for lecture
| 1 | 4; 2 |
---------------------------
这是实际的 PHP 部分:
<?php include ('connect.php'); ?>
<?php
$classes = "SELECT * FROM students, lectures, classes WHERE students.student_id=classes.student_id AND lectures.lecture_id=classes.lecture_id";
$class_result = mysqli_query($con, $classes);
?>
HTML/PHP 部分:
<html>
<title>Classes</title>
<body>
<table border="1" cellspacing="0" cellpadding="10" width="500">
<tr>
<td colspan="2" align="right">
<a href="add_class.php">Add Class</a>
</td>
</tr>
<tr>
<th>Student</th>
<th>Lecture</th>
</tr>
<?php while($class = mysqli_fetch_array($class_result)) { ?>
<tr>
<td align="center"><a href="student.php?student_id=<?=$class['student_id']?>"><?=$class['student']?></a></td>
<td align="center"><a href="lecture.php?lecture_id=<?=$class['lecture_id']?>"><?=$class['lecture']?></a></td>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php include('close.php')?>
这些是表格:
----------Classes---------- // wanted to ----------Classes----------
| Student | Lecture | be like | Student | Lecture |
| John | Math | this | | Math; |
| John | Literature | ---> | John | Literature;|
| Paul | Math | clickable | | Physics; |
| Paul | Speech | each | | Speech |
| Paul | Literature | lecture ---------------------------
| John | Physics | | | Math; |
| Paul | Physics | | Paul | Speech; |
| John | Speech | | | Literature;|
--------------------------- | | Physics |
---------------------------
【问题讨论】:
-
您可以使用
explode(';', $class['grouped']),但您应该知道group_concat有大小限制,我也会去掉分隔符中的那个空间,并为该字段设置别名。GROUP_CONCAT(lecture SEPARATOR '; ') AS grouped或者您可以按您要分组的字段对其进行排序... -
如果您使用 PDO,我会使用
FETCH_GROUP。对于 mysqli 我不确定。
标签: php mysql select mysqli many-to-many