索引
Sorting Retrieved Data
mysql> SELECT * -> FROM manga -> ORDER BY manga_name; +----------+-----------------------+-------------------+--------------+ | manga_id | manga_name | manga_discription | manga_status | +----------+-----------------------+-------------------+--------------+ | 1005 | 2asdasds | dasd | 0 | | 1006 | 3dasdas) | 别出心裁 | 0 | | 1007 | 4444444444 | 3333 | 0 | | 1004 | dasda | 23123 | 0 |
mysql> SELECT * -> FROM manga -> ORDER BY manga_name, manga_discription; # 先按 name 排,然后按 discription +----------+-----------------------+-------------------+--------------+ | manga_id | manga_name | manga_discription | manga_status | +----------+-----------------------+-------------------+--------------+ | 1005 | 2asdasds | dasd | 0 | | 1006 | 3dasdas) | 别出心裁 | 0 | | 1007 | 4444444444 | 3333 | 0 | | 1004 | dasda | 23123 | 0 |
mysql> SELECT * -> FROM manga -> ORDER BY manga_name DESC, manga_discription; # 第一次排是降序(Z~A),第二次(内部排)还是正常序(A~Z) +----------+-----------------------+-------------------+--------------+ | manga_id | manga_name | manga_discription | manga_status | +----------+-----------------------+-------------------+--------------+ | 1000 | 至不死的你 | 以为是哲学 | 0 | | 1001 | 烙印勇士 | 好看到哭 | 0 | | 1002 | 幸福(happiness) | 别出心裁 | 0 | | 1003 | 东京食尸鬼 | 美食动漫 | 0 |
Using a combination of ORDER BY and LIMIT, it is possible to find the highest or lowest value in a column. The following example demonstrates how to find the value of the most expensive item:
SELECT prod_price FROM products ORDER BY prod_price DESC LIMIT 1;
Position of ORDER BY Clause When specifying an ORDER BY clause, be sure that it is after the FROM clause. If LIMIT is used, it must come after ORDER BY. Using clauses out of order will generate an error message.
SQL Versus Application Filtering
Data can also be filtered at the application level. To do this, the SQL SELECT statement retrieves more data than is actually required for the client application, and the client code loops through the returned data to extract just the needed rows.
As a rule, this practice is strongly discouraged. Databases are optimized to perform filtering quickly and efficiently. Making the client application (or development language) do the database's job dramatically impacts application performance and creates applications that cannot scale properly. In addition, if data is filtered at the client, the server has to send unneeded data across the network connections, resulting in a waste of network bandwidth resources.
The WHERE Clause Operators
mysql> SELECT manga_id, manga_name -> FROM manga -> WHERE manga_name='big'; +----------+------------+ | manga_id | manga_name | +----------+------------+ | 1008 | BIG | | 1009 | big | | 1010 | Big | | 1011 | BiG | +----------+------------+ 4 rows in set (0.00 sec)
By default, MySQL is not case sensitive when performing matches, and so fuses and Fuses matched.
不过需要注意的是 LIKE + '通配符语句' 是 case-sensitive (大小写敏感的)
/
mysql> SELECT manga_id, manga_name -> FROM manga -> WHERE manga_id BETWEEN 1005 AND 1007; +----------+------------+ | manga_id | manga_name | +----------+------------+ | 1005 | 2asdasds | | 1006 | 3dasdas) | | 1007 | 4444444444 | +----------+------------+ 3 rows in set (0.00 sec)
/
mysql> SELECT manga_id, manga_name -> FROM manga -> WHERE manga_name IS NULL; Empty set (0.00 sec)
网上有建议说 尽可能设定默认值而不是用 NULL