Php 如何使用mysql从多个类别中选择行,并显示包含每个类别数据的列表?

Php 如何使用mysql从多个类别中选择行,并显示包含每个类别数据的列表?,php,mysql,Php,Mysql,假设我有这样一个数据库设计: 有关数据: id | title | category 1 | first title | 1 2 | second title | 4 3 | third title | 5 4 | fourth title | 2 5 | fifth title | 3 6 | other title | 1 至于类别: id | name 1 | music 2 | movies 3 | funny 4 | people 5

假设我有这样一个数据库设计:

有关数据:

id |    title     | category
1  | first title  | 1
2  | second title | 4
3  | third title  | 5
4  | fourth title | 2
5  | fifth title  | 3
6  | other title  | 1
至于类别:

id | name 
1  | music 
2  | movies
3  | funny 
4  | people 
5  | sport 
如何输出每个类别的列表以及该类别中的项目标题

有些人认为:

<div>
<h1>music</h1>
<div>first title</div>
<div>other title</div>
<div>

<div>
<h1>movies</h1>
<div>fourth title</div>
<div>

// and so on...

音乐
第一个标题
其他标题
电影
第四名
//等等。。。
我不知道如何为这样的结果制定mysql查询

谁能给我提个建议吗?(我希望这个例子很容易理解)

提前谢谢

编辑: 我使用了Eugen Rieck的代码,但我不知道如何做以下事情

我怎样才能同时按
categories.id
titles.id
排序,并最终使用where条件

类似这样的内容:
orderbytitles.id DESC LIMIT 0,其中titles.views>10

SELECT
    t.title,
    c.name
FROM
    titles t
INNER JOIN categories c
    ON c.id = t.category
ORDER BY
    c.name
ASC
这将为您提供按类别排序的所有标题的列表。您在PHP中所做的大致如下:

$prevCategory = null;

while(fetchRows)
{
    $newCategory = $row['name'];

    if($prevCategory != $newCategory)
    {
        echo '<ul>{category name}</ul>';
    }

    echo '<li>{title}</li>';

    $prevCategory = {category name};

}
$prevCategory=null;
while(获取行)
{
$newCategory=$row['name'];
如果($prevCategory!=$newCategory)
{
回音“
    {category name}
”; } 回音“
  • {title}
  • ”; $prevCategory={category name}; }
    请注意,这是半伪代码。它不关闭以前的UL,也不符合您寻求的HTML格式。这会让你走的

    这将为您提供按类别排序的所有标题的列表。您在PHP中所做的大致如下:

    $prevCategory = null;
    
    while(fetchRows)
    {
        $newCategory = $row['name'];
    
        if($prevCategory != $newCategory)
        {
            echo '<ul>{category name}</ul>';
        }
    
        echo '<li>{title}</li>';
    
        $prevCategory = {category name};
    
    }
    
    $prevCategory=null;
    while(获取行)
    {
    $newCategory=$row['name'];
    如果($prevCategory!=$newCategory)
    {
    回音“
      {category name}
    ”; } 回音“
  • {title}
  • ”; $prevCategory={category name}; }

    请注意,这是半伪代码。它不关闭以前的UL,也不符合您寻求的HTML格式。这将帮助您完成操作。

    假设第一个表名为
    titles
    ,第二个表名为
    categories
    ,请使用如下查询

    SELECT categories.name as cat, tiltes.title as title
    FROM categories INNER JOIN titles ON titles.category=categories.id
    ORDER BY categories.id
    
    然后做一个组更改循环

    define('MAX_TITLES_PER_CAT',10);
    
    $cat='';
    $tcount=0;
    while (true) {
      //Fetch a row depending on your DB framework, eg. mysql_fetch_row()
      if ($row['cat']!=$cat) {
        if ($cat!='') echo '</div>';
        $cat=$row['cat'];
        $tcount=MAX_TITLES_PER_CAT;
        echo "<div><h1>$cat</h1>";
      }
      if ($tcount--<=0) continue;
      echo '<div>'.$row['title'].'</div>';
    }
    if ($cat!='') echo '</div>';
    
    define('MAX_TITLES_PER_CAT',10);
    $cat='';
    $t帐户=0;
    while(true){
    //根据您的数据库框架获取一行,例如mysql\u Fetch\u row()
    如果($row['cat']!=$cat){
    如果($cat!='')回显“”;
    $cat=$row['cat'];
    $t数量=每只猫的最大标题数;
    回声“$cat”;
    }
    
    如果($t计数--假设第一个表名为
    titles
    ,第二个表名为
    categories
    ,请使用如下查询

    SELECT categories.name as cat, tiltes.title as title
    FROM categories INNER JOIN titles ON titles.category=categories.id
    ORDER BY categories.id
    
    然后做一个组更改循环

    define('MAX_TITLES_PER_CAT',10);
    
    $cat='';
    $tcount=0;
    while (true) {
      //Fetch a row depending on your DB framework, eg. mysql_fetch_row()
      if ($row['cat']!=$cat) {
        if ($cat!='') echo '</div>';
        $cat=$row['cat'];
        $tcount=MAX_TITLES_PER_CAT;
        echo "<div><h1>$cat</h1>";
      }
      if ($tcount--<=0) continue;
      echo '<div>'.$row['title'].'</div>';
    }
    if ($cat!='') echo '</div>';
    
    define('MAX_TITLES_PER_CAT',10);
    $cat='';
    $t帐户=0;
    while(true){
    //根据您的数据库框架获取一行,例如mysql\u Fetch\u row()
    如果($row['cat']!=$cat){
    如果($cat!='')回显“”;
    $cat=$row['cat'];
    $t数量=每只猫的最大标题数;
    回声“$cat”;
    }
    
    如果($t帐户--非常感谢!您的这段代码使我免于多次查询,现在我还可以添加分页。再次感谢!但是,我有一个问题…我如何排序
    类别.id
    标题.id
    最终使用where条件?您能帮助我吗?只需将它放在查询中:
    选择…from…W此处按categories.id、titles.id排序-不过,排序时要将类别保持在一起,这一点很重要。
    选择categories.cat\u name作为cat,videos.id作为video\u id,videos.title作为video\u title,videos.views作为video\u视图从类别内部加入视频上的视频。categories=categories.cat\u id按catego排序ries.cat_id,videos.id LIMIT 10
    使用此选项仅显示第一个类别中的10个项目。它显示10行-这意味着它显示10个标题,无论它们属于哪个类别。非常感谢!您的此代码使我免于多次查询,现在我还可以添加分页。再次感谢!但是,我有一个问题…如何订购
    categories.id
    titles.id
    最终会使用where条件?你能帮我吗?只需将其放入查询中:
    SELECT…FROM…where ORDER BY categories.id,titles.id
    -不过,排序时要将这些类别保持在一起,这很重要。
    SELECT categories.cat\u name as cat,videos。id为video\u id,videos.title为video\u title,videos.views为video\u视图从类别内部加入视频。category=categories.cat\u id按类别排序。cat\u id,videos.id LIMIT 10
    使用此选项仅显示第一个类别中的10个项目。它显示10行-这意味着它显示10个标题,无论它们属于哪个类别g至。