SQL 二 (有关排序、模糊查询)

来源:互联网 发布:杭州少儿编程培训机构 编辑:程序博客网 时间:2024/06/10 17:12

转自 http://blog.csdn.net/yangyi22/article/details/5411598 && http://dev.mysql.com/doc/refman/5.1/zh/column-types.html && http://hi.baidu.com/pingchengdafei/blog/item/e882d623f4bb464dad34dea2.html


一、给定一张表1,根据要求进行排序:

+--------+----------+---------+----------+
| emp_id | emp_name | dept_id | emp_wage |
+--------+----------+---------+----------+
|   1001 | toni     |       1 |    32000 |
|   1002 | lMcy     |       3 |     2500 |
|   1003 | toM      |       4 |     3200 |
|   1004 | mamy     |       2 |     9500 |
+--------+----------+---------+----------+

 CREATE TABLE employee(empid INT NOT NULL AUTO_INCREMENT,emp_name CHAR(64) NOT NULL,dept_id INT NOT NULL,emp_wage INT NOT NULL,PRIMARY KEY(empid));INSERT INTO employee(empid, emp_name, dept_id, emp_wage) VALUES("1001", "toni", "1", "32000");INSERT INTO employee(empid, emp_name, dept_id, emp_wage) VALUES("1002", "IMcy", "3", 2500);INSERT INTO employee(empid, emp_name, dept_id, emp_wage) VALUES("1003", "toM", "4", 3200);INSERT INTO employee(empid, emp_name, dept_id, emp_wage) VALUES("1004", "mamy", "2", "9500");

1.根据emp_wage,员工工资升序排序:

SELECT * FROM employee ORDER BY emp_wage ASC;

+--------+----------+---------+----------+
| emp_id | emp_name | dept_id | emp_wage |
+--------+----------+---------+----------+
|   1002 | lMcy     |       3 |     2500 |
|   1003 | toM      |       4 |     3200 |
|   1004 | mamy     |       2 |     9500 |
|   1001 | toni     |       1 |    32000 |
+--------+----------+---------+----------+

 

2.根据emp_id降序排序:

SELECT * FROM employee ORDER BY empid DESC;

+--------+----------+---------+----------+
| emp_id | emp_name | dept_id | emp_wage |
+--------+----------+---------+----------+
|   1004 | mamy     |       2 |     9500 |
|   1003 | toM      |       4 |     3200 |
|   1002 | lMcy     |       3 |     2500 |
|   1001 | toni     |       1 |    32000 |
+--------+----------+---------+----------+

 

二、根据以上表1,根据要求进行模糊查询

1、不区分大小写查询员工名中含有m的员工资料

SELECT * FROM employee where emp_name like '%m%';

+--------+----------+---------+----------+
| emp_id | emp_name | dept_id | emp_wage |
+--------+----------+---------+----------+
|   1002 | lMcy     |       3 |     2500 |
|   1003 | toM      |       4 |     3200 |
|   1004 | mamy     |       2 |     9500 |
+--------+----------+---------+----------+

 

2、查询员工名中含有大写M的员工资料:

SELECT * FROM employee where emp_name like binary('%M%');

+--------+----------+---------+----------+
| emp_id | emp_name | dept_id | emp_wage |
+--------+----------+---------+----------+
|   1002 | lMcy     |       3 |     2500 |
|   1003 | toM      |       4 |     3200 |
+--------+----------+---------+----------+

 

3、查询员工名中以“t”打头以“i”结尾的员工资料

SELECT * from employee where emp_name like 't%' and emp_name  like '%i';

+--------+----------+---------+----------+
| emp_id | emp_name | dept_id | emp_wage |
+--------+----------+---------+----------+
|   1001 | toni     |       1 |    32000 |
+--------+----------+---------+----------+

 

4、查询员工名中第三位是大写“M”的员工资料:

SELECT * from employee where emp_name like binary('%__M%');

+--------+----------+---------+----------+
| emp_id | emp_name | dept_id | emp_wage |
+--------+----------+---------+----------+
|   1003 | toM      |       4 |     3200 |
+--------+----------+---------+----------+


以下为说明:

=============================================================================

BINARY不是函数,是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区分大小写
如下:
mysql> select binary 'ABCD'='abcd' COM1, 'ABCD'='abcd' COM2;
+--------+-----------+
| COM1 | COM2 |
+--------+-----------+
|      0     |      1    |
+---------+-----------+
1 row in set (0.00 sec)
=============================================================================
(仅仅有些而已!4.*以前)
因为有的MySQL特别是4.*以前的对于中文检索会有不准确的问题,可以在检索的时候加上binary。
建表:

create TABLE usertest ( id int(9) unsigned NOT NULL auto_increment, username varchar(30) NOT NULL default '', primary key (id) ) 
插入数据:
insert into usertest (username) VALUES('美文'); insert into usertest (username) VALUES('美国项目'); insert into usertest (username) VALUES('李文'); insert into usertest (username) VALUES('老唐'); insert into usertest (username) VALUES('梦漂'); insert into usertest (username) VALUES('龙武'); insert into usertest (username) VALUES('夏');

例如:
select * from usertest where username like '%夏%' 
结果七条记录都出来了,比较郁闷。

如果使用=而不是like的时候,

select * from usertest where username = '夏' 
只出现一个结果。因为mysql 的LIKE操作是按照ASCII 操作的,所以LIKE的时候是可能有问题的。问题继续:如果再加上:
insert into usertest (username) VALUES('文');insert into usertest (username) VALUES('唐');
还是使用select * from usertest where username = '夏' ,结果还是出现3条记录,又郁闷了。解决办法如下:


1.在create的时候就使用binary,而不是在query的时候加。
username varchar(30) BINARY NOT NULL default '', 如果表已经建好了,使用:
alter table usertest modify username varchar(32) binary; 来就该表的属性。

2.在query的时候加上binary,select * from usertest where username like binary '%夏%' ,就可以准确的查询出一条记录来。