--SELECT
--单列
select list_name from table_name;
--多列
select list_name1,list_name2 from table_name;
--所有列
select * from table_name;
--检索列中不同值,distinct必须在列前面,作用全部,如果有多列,每个列都是不同值
select distinct list_name from table_name;
--在MySQL中,用limit 限制n行数据
select list_name from table_name limit n;
--同样在MySQL中能用Limit m offset n 表示第m-1行起的n行;同数组,第一行是0
select list_name from table_name limit m offset n;
/*sort
order by
order by 必须在语句最后*/
--按单列排序
select list_name from table_name order by another_list;
--按多列排序,先按list_1,然后list_2,即list1相同才按list_2
select list_name from table_name order by list_1 ,list_2;
--降序单列排序,desc降序,asc升序,默认是升序
select list_name from table_name order by another_list desc;
/*降序多列,折柳desc只作用list_1,而list_2没有。
想要list_2也升序,需要后面加入desc,即每个list对应一个desc*/
select list_name from table_name order by list_1 desc,list_2;
--where
select list_name from table_name where --condition
/*where字句不同于java的操作符
between 在指定的2个值之间,之间and连接
is null 为null值,是没有值,不是等于0!*/
--in,可以把in看作or,只要list_1满足in里面的一个变量即可
--in一般比or更快,且可以包含select语句
select list_name from table_name where list_1 in('value1','value2',....)
--not从不单独使用
--like通配符
--% %表示任何字符出现任意次
--like 'm%',以m开头的内容,'' 里面的内容区分大小写
select list_name from table_name where another_list like 'm%';
--like '%abc%' 包含abc的内容
select list_name from table_name where another_list like '%abc%';
--like 'a%c' 以a开头c结尾
select list_name from table_name where another_list like 'a%c';
-- like '%' 不代表匹配null
--下划线_ 通配符,只匹配一个字符
select list_name from table_name where another_list like '__ test';
--若有a test和ab test,只匹配ab test,因为语句中有2个_
--[ab]用来匹配a或者b,目前只有微软的Access和SQL Server支持
--在MySQL中,将2个表拼接起来用Concat(),其他数据中有用+ 也有用||
select Concat(list_name1,'(',list_name2,')') from table_name;
--此时第二列用括号包围,但是两列之间有空格
/*RTrim()去掉字符串右边空格
*LTrim()去掉字符串左边空格
*Trim()去掉字符串左右两边空格*/
select RTRIM(RConcat(list_name1,'(',list_name2,')')) from table_name;
--alias 利用as关键字赋别名,在上述所说,将2个结合在一起放在一个新的列,as定义别名
select CONCAT(list_name1,'(',list_name2,')') as alias_list_name from table_name;
--算术运算,将list_name1 与list_name2相乘在新列
select list_name1 * list_name2 as alias_list_name from table_name