php - mysql, ASC 现有mysql查询的数据

标签 php android mysql

如果我有此查询,我需要有关此数据的帮助

SELECT count(*),username
FROM products
WHERE
description LIKE '%Yes%'
or
description LIKE '%yes%'
GROUP BY username

我需要对这些数据进行升序排列,这样我的数据才会显示为这个

username    |    description
 a          |      3
 b          |      1

这是我的 PHP 代码,我想在我的 Android 应用程序上显示它。

 $result = mysql_query("

 SELECT count(*),username
 FROM products
 WHERE
 description LIKE '%Yes%'
or
description LIKE '%yes%'
GROUP BY username

" ) or die(mysql_error()); 



// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();

while ($row = mysql_fetch_array($result)) {
    // temp user array
   $product = array();
    $product["username"] = $row["username"];
    $product["description"] = $row["count(*)"];

    array_push($response["products"], $product);

}
// success
$response["success"] = 1;

// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";

// echo no users JSON
echo json_encode($response);
}

非常感谢您的帮助!!

最佳答案

如果可行,试试这个

  $sql = "SELECT username,count(*) as description
  FROM products
  WHERE
  description LIKE '%Yes%'
  or
 description LIKE '%yes%'
 GROUP BY username
 ORDER BY description ASC";

 $q = mysql_query($sql);
 echo mysql_num_rows($q);
 $output = "<table>";
   $output .= "<td>";
   $output .= "Description";
   $output .= "</td>";
    $output .= "<td>";
   $output .= "Username";
   $output .= "</td>";
   $output .= "</tr>";
 while($row = mysql_fetch_assoc($q)){
   $output .= "<tr>";
   $output .= "<td>";
   $output .= $row["description"];
   $output .= "</td>";
   $output .= "<td>";
   $output .= $row["username"];
   $output .= "</td>";

   $output .="</tr>";
   }

   $output .="</table>";
   echo $output;

如果你想让它在 json 中,最后将它发送到 android

  $sql = "SELECT username,count(*) as description
  FROM products
  WHERE
  description LIKE '%Yes%'
  or
 description LIKE '%yes%'
 GROUP BY username
 ORDER BY description ASC";

 $q = mysql_query($sql);
    $q = mysql_query($sql);
    $product = array(); 
    while ($row = mysql_fetch_array($q))
    { 


    $product[]["username"] = $row["username"];
    $product[]["description"] = $row["description"];
    }
    echo json_encode($product);

这会给你这样的输出

 [{"username":"a"},{"description":"3"},{"username":""},{"description":"1"}] 

如果您想根据用户名而不是描述进行排序,请将查询更改为

 $sql = "SELECT username,count(*) as description
  FROM products
  WHERE
  description LIKE '%Yes%'
  or
 description LIKE '%yes%'
 GROUP BY username
 ORDER BY username ASC";

关于php - mysql, ASC 现有mysql查询的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26575711/

相关文章:

javascript - 使用 Laravel 测试时是否启用了 JS?

android - 处理 webview 的后退和前进按钮?

mysql - WordPress mysql 查询

php - 插入选择 session 客户

javascript - pdo 调用来填充变量以供稍后使用

javascript - 重构数组

android - 如何在 Kotlin 中制作双向转换器? (在java中工作)

android - 将数据从服务器发送到特定的 Android 设备

php - Mysqli 在 php 中插入失败

mysql 查询 : null values behavior when left joining