gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:55:28 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com