MySQL 基本操作--DML:数据操作语句(insert、delete、update)

插入语句(intsert)

添加:

添加一条数据:insert into 表名(字段名1,字段名2,字段名3...) values(值1,值2,值3);

批量添加数据:insert into 目标表名(字段名1,字段名2,字段名3...) select 字段名1,字段名2,字段名3... from 源表


更改:update 表名 set 字段名1=字段名1的值,字段名2=字段名2的值,.. where 条件的逻辑运算符


删除:delete from 表名 where 条件的逻辑运算符


清空表:truncate table 表名;


查询语句(select)

显示所有的字段:select * from 表名;
有选择的显示字段:select 字段名1,字段名2,字段名3... from 表名;
设置字段的别名:select 字段名 别名 from 表名; 或 select 字段名 as 别名 from 表名;

支持算术运算
select shop_id,shop_name,shop_price+100 as '价格' from t_shop_info;
select 10*100 as total;

distinct去除重复行:select distinct 字段名1,字段名2  from 表名;(对于字段1去除重复行)

between区域查询:select * from 表名 where 字段名 between xxx and yyy;
select * from t_shop_info where shop_price between 1000.00 and 2000.00;

like模糊查询:select * from 表名 where 字段名 like '小%';(以 小 字开头的字符串)

in关键字:select * from t_shop_info where shop_id in(2,4);(shop_id等于2或4的显示)
 select * from t_shop_info where shop_id in
(
select _id from ids_table where _id=2 or _id=3
);

order by排序:select * from表名 ordery by 排序字段名;(默认升序)
select * from表名 ordery by 排序字段名 desc;(降序)

char_length:获取字段中字符的个数(数据.length)
select  char_length(字段名) from 表名;

PS:

select curdate(); 获取系统当前的日期(2017-09-15)
select curtime(); 获取系统当前时间(16:33:55)
select now();获取系统的日期及时间(2017-09-15 16:34:21)

select count(*) from 表名; 获取表数据的总行数
select max(字段)
select min(字段)
select sum(字段)

select concat(shop_name,'xxxx') from t_shop_info; //连接字符串
select concat('yyyy',shop_name) from t_shop_info; //连接字符串

支持if判断

ifnull(expr1,expr2):如果expr1为null,则返回expr2,否则返回expr1
nullif(expr1,expr2):如果expr1和expr2相等,则返回null,否则返回expr1
if(expr1,expr2,expr3):有点类似于?:三目运算符,如果expr1为true,不等于0,且不等于null,
                             则返回expr2,否则返回expr3.
isnull(expr1):判断expr1是否为null,如果为null则返回true,否则返回false

case函数:

select 字段2
when 1 then 'JAVA老师'
when 2 then 'Ruby老师'
else '其它老师'
end as 老师类型
from 表名;

select when 逻辑语句 then 'JAVA老师'
when 逻辑语句 then 'Ruby老师'
else '其它老师'
end as 老师类型
from 表名;

group by 关键字

概念:分组查询
语法: group by 字段名 having 分组条件
分组条件:将字段名数据相同相应行归纳成一组
注意问题:不要将非分组字段名,写到select后面
一般配合:
count(*)
sum(字段名)
min(...)
avg(...)
max(字段名)

多表查询

1、交叉多表查询(笛卡儿) select * from 表a,表b;
2、右连接查询
语法:select 字段 from table1 right join table2 on 查询条件
概念:table2的数据根据查询条件如果没有在table2有对应的值,那么table1就会以null值显示
3、左连接查询 
语法:select 字段 from table1 left join table2 on 查询条件
概念:table1的数据根据查询条件如果没有在table2有对应的值,那么table2就会以null值显示
4、全连接查询
语法:select 字段 from table1 full join table2 on 查询条件 
mysql中的用法:
select 字段 from table1 right join table2 on 查询条件 
union
select 字段 from table1 left join table2 on 查询条件;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值