0

我正在尝试获取我在桌子上的所有详细信息,但将它们按两个不同的列名一个接一个地分组,并从 php 中获取相同的

例子:

   id   name    url     category    subcategory
/*------------------------------------------------------/*
    1   google  g.gl    partner     associative
    2   fb      f.b     sponsor     main
    3   yahoo   y.ho    partner     color
    4   stack   st.ck   sponsor     main
    5   mys     l.my    sponsor     sub
    6   bang    b.lo    sponsor     parent
    7   xyz     x.yz    partner     color

我终于想要什么

       id   name    url     category    subcategory
    /*------------------------------------------------------/*
        1   google  g.gl    partner     associative
        3   yahoo   y.ho    partner     color
        7   xyz     x.yz    partner     color
        2   fb      f.b     sponsor     main
        4   stack   st.ck   sponsor     main
        6   bang    b.lo    sponsor     parent
        5   mys     l.my    sponsor     sub

首先category按id分组排序,subcategory再按subcategory升序分组排序。

另外,我如何使用 php 获取相同的内容

我的尝试:

mysql:

SELECT category, GROUP_CONCAT(subcategory) , GROUP_CONCAT(name) , GROUP_CONCAT(url) 
FROM list
GROUP BY category

PHP:

1. mysql_fetch_array to fetch everything..
2. explode subcategory and every other concated values and then echo them
4

1 回答 1

2

你也可以试试这个代码:

<?php 
$db_resource    = mysqli_connect('Host Name', 'username', 'password');
$dbconn         = mysqli_select_db('Data base Name', $db_resource);
$query = 'SELECT * FROM list order by category , subcategory';
$result = mysqli_query($query);
while($row = mysqli_fetch_array($result))
{
    print_r($row);

}
?>
于 2013-11-21T09:21:11.630 回答