如何对SQL数据库的查询结果进行排序? [英] How can I sort the results of a query of my SQL database?
本文介绍了如何对SQL数据库的查询结果进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要你帮助我
我尝试了几种方法而且我收到错误
排序需要如下:
rate DESC
age ASC
Height DESC
重量ASC
也想按字段排序
如果您能用一个例子来解释,那将是完美的注意到订单的确切方式和地点
I need you to please help me
I tried several ways and I get error
sort want as follows:
rate DESC
age ASC
Height DESC
weight ASC
also would like to sort by fields
if you could explain with an example would be perfect for noticing how and where exactly is the order by
<? Php
$ O ='';
/ / Put the appropriate information:
$ Data = array ('localhost', 'user', 'password');
$ Con = mysql_connect ($ data [0], $ data [1], $ data [2]);
if ($ con) {
$ O = 'Error: Could not connect to server. '. mysql_error ();
echo $ o;
exit;
}
/ / Change the name of the database for yours
$ Db_name = 'database';
if (mysql_select_db ($ db_name, $ con)) {
$ O = 'Error: Could not select database "'. $ Db_name. '". '. mysql_error ();
echo $ o;
exit;
}
$ Table = 'users' / / Change this only if you know what it does.
$ Query = "SELECT * FROM $ table";
$ Where = "WHERE";
$ And = 0;
if (isset ($ _GET ['name']) &&! empty ($ _GET ['name'])) {
$ Where. = "Name LIKE '% $ _GET [name]%'";
$ And = 1;
}
if (isset ($ _GET ['rate'])) {
$ E = explode ('-', $ _GET ['rate']);
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
$ Where. = "Rate BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
if (isset ($ _GET ['age'])) {
$ E = explode ('-', $ _GET ['age']);
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
$ Where. = "Age BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
if (isset ($ _GET ['Height'])) {
$ E = explode ('-', $ _GET ['Height']);
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
$ Where. = "Height BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
if (isset ($ _GET ['weight'])) {
$ E = explode ('-', $ _GET ['weight']);
if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
if ($ and === 1)
$ Where. = "AND";
$ Where. = "Weight BETWEEN $ e [0] AND $ e [1]";
$ And = 1;
}
}
if (isset ($ _GET ['City']) &&! empty ($ _GET ['city'])) {
if ($ and === 1)
$ Where. = "AND";
$ Where. = "City = '$ _GET [City]'";
$ And = 1;
}
if (isset ($ _GET ['Eyes']) &&! empty ($ _GET ['Eyes'])) {
if ($ and === 1)
$ Where. = "AND";
$ Where. = "Eyes = '$ _GET [Eyes]'";
$ And = 1;
}
if (isset ($ _GET ['Hair']) &&! empty ($ _GET ['Hair'])) {
if ($ and === 1)
$ Where. = "AND";
$ Where. = "Hair = '$ _GET [Hair]'";
$ And = 1;
}
if (strlen ($ where)> 6)
$ Query. = $ Where;
$ Result = mysql_query ($ query, $ con);
if ($ result) {
$ Nrows = mysql_num_rows ($ result);
if ($ nrows> 0) {
$ O ='';
while ($ row = mysql_fetch_assoc ($ result)) {
$ O. = "$ Row [image]";
}
$ O. = "";
Else {}
$ O = 'There were no results';
}
Else {}
$ O = 'Error: Unable to run the query. '. mysql_error ($ con);
}
mysql_free_result ($ result);
mysql_close ($ con);
echo $ o. "";
exit;
?>
推荐答案
O = ' ';
/ /放入适当的信息:
O =''; / / Put the appropriate information:
Data = array(' localhost',' user', ' password');
Data = array ('localhost', 'user', 'password');
Con = mysql_connect(
Con = mysql_connect (
这篇关于如何对SQL数据库的查询结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文