我如何在PHP中排序我的id?

时间:2015-05-09 08:15:03

标签: php sorting

我如何在php中对我的ID进行排序我尝试过这样但没有显示出良好的响应 这是我的代码



$sql = "SELECT * FROM Sites ORDER BY id DESC LIMIT 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
		$site_id  = $row["id"];  

			

$result2 = mysql_query("SELECT COUNT(mon_id) FROM status Where site_id='$site_id'");  
while($row2=mysql_fetch_array($result2))  
{  
  
$mon_id = $row2['COUNT(mon_id)']; 
echo $mon_id;?><br /><?php
//try to sort my ids

   $mon_id= rsort($mon_id);
}  }}
&#13;
&#13;
&#13;

得到结果     2     2     1     7     1     1     五     1     2     1

那么如何对这个结果进行排序

    7
    5
    2
    2
    2
    1
    1
    1
    1
    1

4 个答案:

答案 0 :(得分:0)

您的问题是,您正在查询两次,并且对第一个查询结果的每一行执行第二个查询。因此,rsort正在执行10次,但每次只执行一行。

我假设你想要每个网站的状态计数?! 然后您可以改为使用此查询:

SELECT COUNT(status.mon_id) as cnt, status.site_id 
FROM status, sites 
WHERE status.site_id=sites.id 
GROUP BY sites.id 
ORDER BY sites.id, cnt 
DESC LIMIT 10;

此查询首先按sites.id排序,然后按cnt排序,并将结果限制为10。 所以,你只需要一个查询。

插入数据库连接信息

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Keine Verbindung möglich: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT COUNT(status.mon_id) as cnt, status.site_id 
                        FROM status, sites 
                        WHERE status.site_id=sites.id 
                        GROUP BY sites.id 
                        ORDER BY sites.id, cnt DESC 
                        LIMIT 10");  
if($result!=false){
    while($row=mysql_fetch_array($result, MYSQL_ASSOC))  
    {    
        $mon_id = $row['cnt']; 
        echo $mon_id."<br />";        
    }
}else{
    echo "mysql error";
}

答案 1 :(得分:0)

count添加到$result2 = mysql_query("SELECT COUNT(mon_id) as total FROM status Where site_id='$site_id'"); while($row2=mysql_fetch_array($result2)) { $mon_id[] = $row2['total']; } 。试试 -

rsort()

将计数存储到数组中,然后对其实施rsort($mon_id);

 <add name="ConnectionString" 
      connectionString="Data Source=SQL5003.Smarterasp.net;Initial Catalog=DB_9C4561_WFormBlog;User Id=DB_9C4561_WFormBlog_admin;Password=mydbpwd"
      providerName="System.Data.EntityClient" />

答案 2 :(得分:0)

你可以尝试这个

$sql = "SELECT * FROM Sites ORDER BY id DESC LIMIT 10";
$result = $conn->query($sql);

$mon_id = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $result2 = mysql_query('SELECT COUNT(mon_id) FROM status Where site_id='.$row['id']);
        if ($result2->num_rows > 0) {
            while($row2=mysql_fetch_array($result2)) {  
                $mon_id[] = $row2['COUNT(mon_id)'];
            }
        }
    }
    rsort($mon_id);
    foreach ($mon_id as $key => $val) {
        echo "$key = $val\n";
    }
}

答案 3 :(得分:0)

我做了一个小提琴:http://sqlfiddle.com/#!9/4d2c7/7

error_reporting(E_ALL);
ini_set('display_errors', 1);
$sql = 'select *, count(mon_id) as sum_id from Sites as si
        inner join status as st
        on si.id = st.site_id
        group by st.site_id
        order by sum_id desc';

$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
    echo $row['id'] . ' - ' . $row['sum_id'] . '<br/>';
}