Mysql 将两个表的结果分组
我想将以下两个表的结果分组 项目结果 认证标准 使用以下连接/查找表: 课程\结果\认证\标准\查找 结果应该如下所示:Mysql 将两个表的结果分组,mysql,join,Mysql,Join,我想将以下两个表的结果分组 项目结果 认证标准 使用以下连接/查找表: 课程\结果\认证\标准\查找 结果应该如下所示: +-------------------+--------------------------+ | program_outcome 1 | accreditation_standard 2 | | | accreditation_standard 5 | | | accreditation_sta
+-------------------+--------------------------+
| program_outcome 1 | accreditation_standard 2 |
| | accreditation_standard 5 |
| | accreditation_standard 9 |
| | ... |
+-------------------+--------------------------+
| program_outcome 2 | accreditation_standard 3 |
| | accreditation_standard 7 |
| | accreditation_standard 8 |
| | ... |
+-------------------+--------------------------+
等等
到目前为止,我已经尝试:
SELECT
*
FROM
program_outcome,
accreditation_standard,
program_outcome_accreditation_standard_lookup
LEFT JOIN accreditation_standard a_s ON a_s.accreditation_standard_pk = program_outcome_accreditation_standard_lookup.accreditation_standard_fk
LEFT JOIN program_outcome p_o ON p_o.program_outcome_pk = program_outcome_accreditation_standard_lookup.program_outcome_fk
GROUP BY
p_o.program_outcome_pk
但上述查询不是根据上述连接/查找表中的program_Output.program_Output_fk对Accuration_standard.Accuration_standard_fk进行分组
我可能应该使用GROUP_CONCAT,但不确定如何在上面实现。我使用的是MySQL v5.5
看小提琴
我将结果格式化如下:
$result = $connection->query( $query );
echo "<table width='100%' border='1'><thead><tr><th>Program Outcomes</th><th>Accreditation Standards</th></tr></thead><tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['program_outcome'] . "</td><td>" . $row['accreditation_standard'] . "</td></tr>";
}
echo "<tbody></table>";
我不确定映射是否正确,您可以自己修改它
SELECT
CONCAT( 'program_outcome ', p_o.program_outcome_pk ) AS popk,
GROUP_CONCAT( DISTINCT CONCAT( 'accreditation_standard ', a_s.accreditation_standard_pk ) SEPARATOR "\r\n" ) AS aspk
FROM
program_outcome,
accreditation_standard,
program_outcome_accreditation_standard_lookup
LEFT JOIN accreditation_standard a_s ON a_s.accreditation_standard_pk = program_outcome_accreditation_standard_lookup.accreditation_standard_fk
LEFT JOIN program_outcome p_o ON p_o.program_outcome_pk = program_outcome_accreditation_standard_lookup.program_outcome_fk
GROUP BY
p_o.program_outcome_pk
我不确定映射是否正确,您可以自己修改它
SELECT
CONCAT( 'program_outcome ', p_o.program_outcome_pk ) AS popk,
GROUP_CONCAT( DISTINCT CONCAT( 'accreditation_standard ', a_s.accreditation_standard_pk ) SEPARATOR "\r\n" ) AS aspk
FROM
program_outcome,
accreditation_standard,
program_outcome_accreditation_standard_lookup
LEFT JOIN accreditation_standard a_s ON a_s.accreditation_standard_pk = program_outcome_accreditation_standard_lookup.accreditation_standard_fk
LEFT JOIN program_outcome p_o ON p_o.program_outcome_pk = program_outcome_accreditation_standard_lookup.program_outcome_fk
GROUP BY
p_o.program_outcome_pk
这不是一个普通的连接吗。。?希望我能正确理解你的问题:
SELECT program_outcome,
accreditation_standard
FROM program_outcome p
JOIN program_outcome_accreditation_standard_lookup l ON p.program_outcome_pk = l.program_outcome_fk
JOIN accreditation_standard a ON l.accreditation_standard_fk = a.accreditation_standard_pk;
在下面的小提琴中检查结果。或下面的查询。基本上相同的结果只是更接近您的预期结果
SELECT program_outcome,
GROUP_CONCAT(accreditation_standard SEPARATOR "\r\n") AS accreditation_standard
FROM program_outcome p
JOIN program_outcome_accreditation_standard_lookup l ON p.program_outcome_pk = l.program_outcome_fk
JOIN accreditation_standard a ON l.accreditation_standard_fk = a.accreditation_standard_pk
GROUP BY program_outcome;
顺便说一句,你的字段命名选择给我留下了深刻的印象。这不是一个普通的连接吗。。?希望我能正确理解你的问题:
SELECT program_outcome,
accreditation_standard
FROM program_outcome p
JOIN program_outcome_accreditation_standard_lookup l ON p.program_outcome_pk = l.program_outcome_fk
JOIN accreditation_standard a ON l.accreditation_standard_fk = a.accreditation_standard_pk;
在下面的小提琴中检查结果。或下面的查询。基本上相同的结果只是更接近您的预期结果
SELECT program_outcome,
GROUP_CONCAT(accreditation_standard SEPARATOR "\r\n") AS accreditation_standard
FROM program_outcome p
JOIN program_outcome_accreditation_standard_lookup l ON p.program_outcome_pk = l.program_outcome_fk
JOIN accreditation_standard a ON l.accreditation_standard_fk = a.accreditation_standard_pk
GROUP BY program_outcome;
顺便说一句,您的字段命名选择给我留下了深刻的印象。这是前两个答案的最终有效解决方案
$query = "SELECT program_outcome,
GROUP_CONCAT(accreditation_standard SEPARATOR '|') AS accreditation_standards
FROM program_outcome p
JOIN program_outcome_accreditation_standard_lookup l ON p.program_outcome_pk = l.program_outcome_fk
JOIN accreditation_standard a ON l.accreditation_standard_fk = a.accreditation_standard_pk
GROUP BY program_outcome";
$result = $connection->query( $query );
echo "<table width='100%' border='1'><thead><tr><th>Program Outcomes</th><th>Accreditation Standards</th></tr></thead><tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['program_outcome'] . "</td><td>" . str_replace('|', '<p>',$row['accreditation_standards']) . "</td></tr>";
}
echo "<tbody></table>";
这是前两个答案的最终有效解决方案
$query = "SELECT program_outcome,
GROUP_CONCAT(accreditation_standard SEPARATOR '|') AS accreditation_standards
FROM program_outcome p
JOIN program_outcome_accreditation_standard_lookup l ON p.program_outcome_pk = l.program_outcome_fk
JOIN accreditation_standard a ON l.accreditation_standard_fk = a.accreditation_standard_pk
GROUP BY program_outcome";
$result = $connection->query( $query );
echo "<table width='100%' border='1'><thead><tr><th>Program Outcomes</th><th>Accreditation Standards</th></tr></thead><tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['program_outcome'] . "</td><td>" . str_replace('|', '<p>',$row['accreditation_standards']) . "</td></tr>";
}
echo "<tbody></table>";
在MySQL中预期的结果是这样的吗?不,我可以使用PHP和HTML格式化结果…请参阅更新的OPI。我认为您可以简化您的示例,但如果我理解正确,您正在尝试使用pivot,GROUP的方式为每个组获取一行。如果你想把它们放在连续的行上,使用ORDER By谢谢你的信息和建议,我以后一定会看的。现在,我将最终的解决方案放在一个单独的答案中,而不是放在OP中。在MySQL中,预期结果会是这样吗?不,我可以使用PHP和HTML格式化结果…请参阅更新的OPI。我认为您可以简化您的示例,但如果我理解正确,您正在尝试使用pivot、GROUP的方法,即为每个组获取一行。如果你想把它们放在连续的行上,使用ORDER By谢谢你的信息和建议,我以后一定会看的。我现在把最后的解决方案放在一个单独的答案中,而不是放在OP中。两个答案都有效,只需稍作修改,格式就是所需的。我已经更新了OP,以显示具有所需格式的最终工作解决方案。如果可以的话,我会给出两种正确的解决方案,但我选择了最简单的一种……也许你想解释一下为什么像上面评论中提到的@tcadidot0那样使用逗号连接和左连接?抱歉,错过了……混合方法?否则没有理由。@Tenderfoot的“OP”是你。两个答案都有效,只需稍作更改,格式就是必需的。我已经更新了OP,以显示具有所需格式的最终工作解决方案。如果可以的话,我会给出两种正确的解决方案,但我选择了最简单的一种……也许你想解释一下为什么像上面评论中提到的@tcadidot0那样使用逗号连接和左连接?抱歉,错过了……混合方法?否则就没有理由了,“OP”就是你。