试图根据这些学生所在的组对学生进行分组。我试图将结果显示在某种对象/ JSON中,以便以后可以在JS中应用一些排序算法。不确定如何添加WHERE语句或Join语句?
$link = mysqli_init();
$success = mysqli_real_connect( $link, $host, $user, $password, $db, $port);
$query = mysqli_query($link, "SELECT s_id, group_id, name FROM Group, Student");
$table = array();
while($row = mysqli_fetch_assoc($query))
{
$table = $row;
echo json_encode($table);
}
I want result something like
`
[{'Group': 1, 'name': 'john', 'ID': 101},
{'Group': 1, 'name': 'mike', 'ID': 103},
{'Group': 2, 'name': 'alice', 'ID': 102},
{'Group': 2, 'name': 'rachel', 'ID': 104},]`
the "Student" table
s_id class_id name
101 1 john
103 1 mike
102 1 alice
104 1 rachel
the "Group" table
class_id s_id group_id
1 101 1
1 102 2
1 103 1
1 104 2
答案 0 :(得分:0)
使用以下方法更改您的SQL:
SELECT G.group_id AS `Group`,
S.name,
S.s_id AS ID
FROM Student S
JOIN `Group` G ON S.s_id = G.s_id
ORDER BY G.group_id, S.s_id
这是学生ID列的2个表之间的INNER JOIN,我们选择要显示的列,并根据需要对结果进行排序。