MySQL分组问题

时间:2015-04-22 04:51:23

标签: php sql

我想通过Continent显示数据组。

我有下表:

Table: Score

RegionID  | RegionName  | Continent  | Score
----------+-------------+------------+-------
A001      | Hong Kong   | Asia       | 97
A002      | Macau       | Asia       | 94
A003      | Taiwan      | Asia       | 95
A004      | Seoul       | Asia       | 96
A005      | Tokyo       | Asia       | 95
A006      | Osaka       | Asia       | 93
E001      | London      | Europe     | 88
A007      | Beijing     | Asia       | 83
E002      | Paris       | Europe     | 89
U001      | New York    | N. America | 91
U002      | Las Vegas   | N. America | 90
T001      | Sydney      | Australia  | 98

预期产出:

Asia:
-----------------------------------------
97 - Hong Kong
94 - Macau

...

N. America
-----------------------------------------
91 - New York
90 - Las Vegas

现在我必须将SQL命令分开才能输出。

我需要比

更快的解决方案
SELECT * FROM Score WHERE Continent='Asia'
SELECT * FROM Score WHERE Continent='N. America'

4 个答案:

答案 0 :(得分:3)

这不是来自数据库的GROUPED数据。这只是一个排序列表,然后您使用您的编程语言(PHP)来显示分组。

SELECT * FROM Score ORDER BY Continent

现在,当您获得该结果时,您必须使用PHP在新行中显示每个新大陆,然后显示其条目。由于您需要所有行,因此仅使用数据库进行分组将是一种过度杀伤。而且你必须用PHP创建你的大陆标题。

答案 1 :(得分:2)

如果您在一个查询中选择所有内容:

SELECT * FROM Score WHERE Continent IN ('Asia', 'N. America');

然后,在PHP中,您将按notulyssess建议并使用结果集执行:

$data[$row['continent']][] = $row;

最后,您可以循环遍历$data数组:

for($data as $continent => $entries){
    echo '<b>' . $continent . '</b><br />-----------------------------------------<br />';
    for($entries as $e){
        echo $e['score'] . ' - ' . $e['region_name'] . '<br>';
    }
}

答案 2 :(得分:0)

SELECT * FROM Score WHERE Continent='Asia' limit 2
UNION
SELECT * FROM Score WHERE Continent='N. America' limit 2

希望这能帮到你..

答案 3 :(得分:0)

对于提供的解决方案,查询必须是:

SELECT * FROM Score ORDER BY Continent

php演示文稿将是:

<?php

// I've entered the sample data as a array - you'll got it the same after the query.
// I've qvoid the last column from your sample data, because it isn't important for my presentation
$dat=array(
0=>array('RegionID'=>'A001','RegionName'=>'Hong Kong','Continent'=>'Asia'),
1=>array('RegionID'=>'A002','RegionName'=>'Macau','Continent'=>'Asia'),
2=>array('RegionID'=>'A003','RegionName'=>'Taiwan','Continent'=>'Asia'),
3=>array('RegionID'=>'A004','RegionName'=>'Seoul','Continent'=>'Asia'),
4=>array('RegionID'=>'A005','RegionName'=>'Tokyo','Continent'=>'Asia'),
5=>array('RegionID'=>'A006','RegionName'=>'Osaka','Continent'=>'Asia'),
6=>array('RegionID'=>'A007','RegionName'=>'Beijing','Continent'=>'Asia'),
7=>array('RegionID'=>'E001','RegionName'=>'London','Continent'=>'Europe'),
8=>array('RegionID'=>'E002','RegionName'=>'Paris','Continent'=>'Europe'),
9=>array('RegionID'=>'U001','RegionName'=>'New York','Continent'=>'N. America'),
10=>array('RegionID'=>'U002','RegionName'=>'Las Vegas','Continent'=>'N. America'),
11=>array('RegionID'=>'T001','RegionName'=>'Sydney','Continent'=>'Australia'),
);

// Maincode
$sbuf='';
foreach($dat as $da){
    if($da['Continent']!=$sbuff){
        "<br/>".echo $da['Continent']."<br/>-----------------------------------------<br/>";
    }
    echo $da['RegionName']."<br/>";
    $sbuff=$da['Continent'];
}
?>