MySQL中,ASC在现有的MySQL查询的数据 [英] mysql, ASC the data of the existing mysql query
本文介绍了MySQL中,ASC在现有的MySQL查询的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要这个数据帮助,如果我有这个疑问。
I need help on this data if I have this query
SELECT count(*),username
FROM products
WHERE
description LIKE '%Yes%'
or
description LIKE '%yes%'
GROUP BY username
我需要ASC这个数据,这样它会出现我的数据,因为这
I need to ASC this data so that it will appear my data as this
username | description
a | 3
b | 1
这是我的PHP code,我想表明它和我的Android应用中显示。
and this is my PHP code which I want to show it and display it on my android apps.
$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);
}
您的帮助是非常AP preciated !!
Your help is Much appreciated!!
推荐答案
试试这个,如果它的工作原理
try this if it works
$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
if you want this to be in json finally to send it to 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"}]
如果您对于用户名,而不是描写的特征要排序,查询更改为
if you want sort with respect to username instead of descripton, change the query to
$sql = "SELECT username,count(*) as description
FROM products
WHERE
description LIKE '%Yes%'
or
description LIKE '%yes%'
GROUP BY username
ORDER BY username ASC";
这篇关于MySQL中,ASC在现有的MySQL查询的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文