继承我的桌子:
Table: category
----------------
id | category
------------------
1 | category1
2 | category2
3 | category3
4 | category4
5 | category5
-
Table: news
------------------------------
id | title | category_id
------------------------------
1 | title 1 | 1
2 | title 2 | 2
3 | title 3 | 4
4 | title 4 | 5
5 | title 5 | 3
6 | title 6 | 3
7 | title 7 | 2
8 | title 8 | 5
9 | title 9 | 1
10| title 10 | 4
11| title 11 | 1
12| title 12 | 2
如何获得 mysql结果,如下所示:
(注意:每个类别(1-5)仅分配一个标题,然后再次重复类别的循环。)
我尝试过按顺序使用`group by。
Category 1 ===
title 1
Category 2
title
Category 3
title 5
Category 4
title 3
Category 5
title 4
Category 1 ===
title 9
Category 2
title
category 3
title 6
category 4
title10
category 5
title 8
category 1 ===
title 11
category 2
title 12
答案 0 :(得分:2)
如果您想使用PHP构建此类格式,或者您可以使用while循环来创建它。考虑这个例子:
首先,您需要正确查询这些值,您可以使用join
// SAMPLE => SELECT * FROM `category`
// LEFT JOIN `news` ON `news`.`category_id` = `category`.`id`
示例输出:SQL HERE
其次,获取值后,您需要对它们进行排序,在这种情况下,(1, 2, 3, 4, 5)
考虑这个例子:
$results_from_db = array(
array('id' => 1, 'category' => 'category1', 'title' => 'title 1', 'category_id' => 1),
array('id' => 1, 'category' => 'category1', 'title' => 'title 9', 'category_id' => 1),
array('id' => 1, 'category' => 'category1', 'title' => 'title 11', 'category_id' => 1),
array('id' => 2, 'category' => 'category2', 'title' => 'title 2', 'category_id' => 2),
array('id' => 2, 'category' => 'category2', 'title' => 'title 7', 'category_id' => 2),
array('id' => 2, 'category' => 'category2', 'title' => 'title 12', 'category_id' => 2),
array('id' => 3, 'category' => 'category3', 'title' => 'title 5', 'category_id' => 3),
array('id' => 3, 'category' => 'category3', 'title' => 'title 6', 'category_id' => 3),
array('id' => 4, 'category' => 'category4', 'title' => 'title 3', 'category_id' => 4),
array('id' => 4, 'category' => 'category4', 'title' => 'title 10', 'category_id' => 4),
array('id' => 5, 'category' => 'category5', 'title' => 'title 4', 'category_id' => 5),
array('id' => 5, 'category' => 'category5', 'title' => 'title 8', 'category_id' => 5),
);
$ids = array();
foreach($results_from_db as $value) {$ids[] = $value['id'];}
$ids = array_values(array_unique($ids));
sort($ids);
$needle_key = 0;
// build the data (kinda like recursive searching)
$data = array();
while(true) {
$current_value = array_shift($results_from_db);
if($current_value['category_id'] == $ids[$needle_key]) {
$data[] = $current_value;
$needle_key++;
if($needle_key > count($ids)-1) {
$needle_key = 0; // resetter
}
} else {
array_push($results_from_db, $current_value);
}
if(empty($results_from_db)) {
break;
}
}
?>
<!-- if you want to echo -- >
<?php foreach($data as $key => $value): ?>
<?php echo '<strong>'.$value['category'].'</strong><br/>'; ?>
<?php echo str_repeat(' ', 8) . $value['title'].'<br/><br/>'; ?>
<?php endforeach; ?>
应输出如下内容:Sample Fiddle
category1
title 1
category2
title 2
category3
title 5
category4
title 3
category5
title 4
category1
title 9
category2
title 7
category3
title 6
category4
title 10
category5
title 8
category1
title 11
category2
title 12