数据库基础2——语法查询
1.select id,name,price from xy; 查询语句,指定查询xy表中的id,name,price。
2.select id,name,price from xy where price>200000 order by id desc ;将price大于200000的id,name,price,进行降序输出
3.desc xy;查看表xy的结构
4.distinct(去重),select distinct address from xy; 输出xy表中的address列,distinct去掉重复的值
5. select id,name,price from xy wehre id=1;where条件语句,从xy表中查找id=1的id,name,price 输出
6.select id,name,price from xy wehre id in(9,12); where条件,从xy表中,查找id为9和12的列,输出id,name,price。 (in相当于指定查询吧,用于查看多个不连续的数据比较方便)
7.select id,name,price from xy where id between 4 and 9; 从xy表中查看id 4 到9之间的所有id,name,price。 (between 区间查询,例如4 and 9,就是将4,5,6,7,8,9的数据)
8. select id,name,price from xy where id like 2; 使用like查询,这里的like相当于=,使用like查询,从xy表中查询id=2的id,name,price
9. select id,name,price,address from xy where address like ‘上%’; like查询,从xy表中查询以上开头的id,name,price,address (like ‘上’%; 右边的百分号相当于以上开头的所有数据,like ‘%上’;左边百分号相当于以上结尾的所有数据)
10,select id,name,price ,address from xy where address like ‘%所’;从xy表中查找address中所有以所结尾的id,name,price,address。
11. select id,name,price,address from xy where address like ‘长____’;从xy表中查找以长____的数据输出他的id,name,price,address )(长_____ ,这里的意思是指定字符个数,假如,你有一个姓里尔的用户,那么用法就是like ‘里_’;我这里是8个字符,去掉开头那个所以有7个_)
12. select id,name,price from xy where id>6 and price>250000; 从xy表中,查询id>6和price>250000的id,name,price (and查询,两个条件必须都为真)
13. select id,name,price from xy where id>6 or price>250000; 从xy表中,查询id>6 或者price>250000的id,name,price (or查询,一方为真都为真,这里输出了id>6的或price>250000)
14. select id,name,price,address from xy where id>6 and price>250000 or address like ‘长____’; 从xy表中,查询id>6和price>250000或者address 以长开始的8个字符的数据,输出id,name,price,address (这里是优先级,and大于or的优先级,得先进行and语句,在进行or)
15. select id,name,price from xy where id>7 limit,从xy表中,查询id>7的id,name,price ,输出第一条,并且只输出一条 (limit 0,1 ,limit 1,2 这里做个对比,limit 0,1就是查询第一条,并且只输出一条,limit 1,2 就是查询第二条,并且只输出二条)
16. select id,name,price from xy where id<5 order by id desc limit 0,1;这里是一个order by limit联合查询,从xy表中,查询id>5的id,name,price ,输出第一条,并且只输出一条,id进行降序排序
17. select address from xy group by address;分组查询,从xy表中进行分组查询
18.select count() from xy;从xy表中统计行数 (count()统计条数函数,这里我的表中只有13列数据)
19. select avg(price) from xy; select max(price) from xy; select min(price) from xy;这里都是一些函数利用,平均值,最大值。最小值
20.Select * from xy where address regexp ‘^上’;从xy表中查询以上开头的address(^,正则表达式,放在你需要查询的数据前面,表示查询以..开头的数据)
21. Select * from xy where address regexp ‘上,正则表达式,放在你需要查询的数据后面,表示查询以..结尾的数据)
22. Select * from xy where phone regexp ‘[6-8]’; 从xy表中查询phone之间有6-8的数据 ([ ]正则表达式,区间查询)
23. Select * from xy where color regexp ‘灰|炫’; 从xy表中查询color有灰或炫的数据 (|正则表达式,管道查询)
24. Select * from xy where phone regexp ‘3+9’; 从xy表中查询phone 9之前必须有一个3的的数据 (+正则表达式,+前面是必须有的值,后面是参数值)
25. Select * from xy where phone regexp ‘3*9’; 从xy表中查询phone 9之前不管有没有3的的数据 (+正则表达式,)
26. 24. Select * from xy where phone regexp ‘1{2}’; 从xy表中查询phone 之间有出现过两次1的数据 Select * from xy where phone regexp ‘1{2,5}’; 从xy表中查询phone 之间有出现过两次到五次1的数据 (+正则表达式,)
27.select * from xy order by 6;猜字段,已知我表中有6个字段,所以只有是1-6都为正确,7及以上为错,因为没有那么多字段
28.select * from xy union select 1,2,@@version_compilent_os(),version(),database(),user();
Union联合查询,可以看到在字段列插入各种命令,会显示不同的数据
29.查询用户名,select user(); select system_user(); select session_user(); select current_user();
30.select database(); select version(); select @@version_compilent_os() (查询数据库名,查询版本,查询操作系统)
31.截取函数的利用 select left(user(),1)=’r’; 截取左边第一个用户名的字符,等于r就返回1,错误返回0
32.Select right(user(),1)=’t’; 截取右边第一个用户名的字符,等于返回1,错误返回0