mysql基本语法
一、mysql排序
不加任何关键字,默认是按照升序来排序的
排序关键字:desc(降序),asc(升序)
代码:select id,Name,CountryCode,District,Population from city order by Population desc
多字段排序:
select id,Name,CountryCode,District,Population from city order by CountryCode desc,Population asc
二、mysql 通配符
1、%aa%----匹配字符串包含aa的字符串
2、%aa---匹配以aa结尾的字符串
3、aa%-----匹配以aa开头的字符串
4、aa_------匹配字符串aa+任意一个字符
5、去空格函数:rtrim(),ltrim()
mysql字符串匹配忽略大小写问题
三、常用函数
例子:
日期时间函数:
获取当前日期,当前时间
select curdate(),curtime()
获取时间戳形式的日期格式
date()函数:
select date(last_update) from sakila.film where date(last_update)='2006-02-15'
date().year(),month(),datediff()函数
四、汇总函数
1、distinct:过滤出唯一列
select distinct filed1,field2 from 表
过滤出filed1,field2组成的唯一字段
2、分组函数
分组过滤
3、子查询:
查询结果作为表
4、表关联
inner join:
union,union all关键字
union:将两个表查询出来的的数据合并为同一个表(去掉重复)
union all:不去重复
表复制
create table if not exists aa
as select * from city;
删除表
drop table if exists aa
表定义:
基本的数据类型
create table test(
id int,
name varchar(30),
age int,
language varchar(50),
primary key(id)
)
添加列:
alter table test add new_name varchar(100)
删除列:
alter table 表名 drop 列名
修改字段类型:
alter table test MODIFY name varchar(300)
修改字段名称:
alter table test change name nname varchar(300)
四、null值说明