1、查看一个表中的数据
select * from 表名;
2、起别名 使用 as 或 使用 空格(如果别名有特殊符号,可使用引号)
select name as _name from user;
或
select name _name from user;
3、常见的补充函数:
(0)select version( ); 获取当前版本号。
(1)select database( );显示当前的数据库。
(2)select user( );显示当前的用户。
(3)select ifnull(字段名, 表达式);判断如果这个字段为null,则返回参数二表达式,字段如果不为null,则原样返回。
(4)select length(字符或字段); 获取一个字段或字符长度。
==================================================================
mysql语句:
一、基础查询:
以user表为例:
1、查询常量
例:select 1;
2、查询表达式
例:select 2*2;
3、查询单个字段
例:select name from user;
4、查询多个字段
例:select name,age,height from user;
5、查询所有字段
例:select * from user;
6、多字段拼接, 拼接函数 - concat( )
例:select concat(first_name, last_name) gen_name from user;
7、去重查询 - distinct
例:select distinct age from user;
过滤掉年龄相同的:
补充:
distinct只能在所有字段的最前边,当distinct后边有多个字段时,表示的意思为:后边的所有字段联合起来去重:
select distinct name, age from user 这句话意思为 过滤掉name和age 相同的数据,表里边有3条记录,其中2条一样的,使用distinct就会过滤掉重复的一条。
案例:统计岗位的数量
如果对distinct不了解,可能会通过对岗位的分组然后进行统计岗位数量的统计,但是这道题使用distinct 就很高效的统计出来:
select count(distinct 岗位) from 表;
8、查询一个表所有字段的类型及约束情况:desc 表名;或 show columns from 表名;
二、条件查询:
语法:select 查询字段 from 表名 where 筛选条件;
执行顺序:先执行 from子句 -> where子句 -> select子句
1、按条件表达式筛选:> 、< 、>= 、<= 、 = 、 !=、<> 等价于 !=
查询年龄不等于20的数据:
例:select user.name from user where age <> 20;
2、按逻辑表达式筛选:and 、or、not 也可以使用 && 、||、 !
3、模糊查询:
like :通常搭配通配符一起使用,_ 表示任意单个字符;% 表示任意多个字符。
in :查询某字段的值是否属于指定的列表之内。例 a in ( xxx, xxx, xx, xx, xxx ) 括号里的值是否与a相同的
between x and y 查询在x和y之间的数据; not between x and y 查询不在x和y之间的数据;
is null 查询是否的数据 / is not null 查询不是否的数据
性能优化方面:
在储存数据时,非null字段的处理要比null字段的处理高效些,且不需要判断是否为null。
null在mysql中,不好处理,储存需要额外的空间,运算和需要特殊的运算符,只能通过 is null 和 is not null 来判断字段是否为null。
通常使用特殊的数据进行站位或表示: 比如 数据类型为int时,如果没有数据,则用0来表示;数据类型为字符串string时,则用 '' 空字符串来表示。
(1)查询名字中含有 三 的数据:
select * from user where name like '%三%';
(2)查询名称中第三个字符是_的数据,只需用转义字符 \ 换一下_ 即可:
select * from user where name like '__\_%';
下边查出来的这条数据是临时再表中添加的。
补充小知识点:转义字符可以使用 escape 关键字自定义
例: 让$ 符号成为转义字符。
(3)查询年龄 为20或23或25的数据:
4、按关系表达式筛选:
例:查询年龄不是20-23之间的数据:
select * from user where age not between 20 and 23;
三、向表中插入数据:
语法:insert [into] 表名 [(列名1, 列名2, 列名3)] values ( 值1, 值2, 值3 ... );
其中[ ] 内容是可选的。
1、向user表中插入一条数据:
insert into user (id, name, age, height, first_name, last_name) values ( 20, ' 测试名', 100, 1.80, '测', 'dlks' )
简写: insert user ( 20, ' 测试名', 100, 1.80, '测', 'dlks' )
也可以插入多条数据: insert user (10, '测试‘', 100), (20, '测试1', 200), ( ... ) ...
2、向user表中插入部分数据:
insert into user (name, age) values( '测试', 200 )
四、更新表中的数据:
语法:update 表名 set 字段 = 新值 where 更新条件
1、更新 user 表中id为21的数据中的name字段:
update user set name = '测试2' where id = 21;
2、把user表中age字段 增加1:
update user set age = age +1;
3、把user表中id为21的数据,name字段值 改为1,age 改为 30:
update user set name='1', age=30 where id = 21;
五、删除表中的数据:
语法:delete from 表名 where 删除条件;
注意:使用delete 删除一张大表时,非常慢,因为它不会把数据所在的物理空间给删除掉,数据是可以回滚的。
如果你想彻彻底底删除一张表,可以使用 truncate , 它会把表头以下的数据全部删除掉,只剩下表头字段,这个删除是不可回滚的:
truncate table 表;
1、删除user表中id为21的数据:
delete from user where id = 21
六、对表的修改:
语法:alert table 语句
1、添加列(默认插到表的最后):
语法: alter table 表名 add 列名 列数据类型 [ after 插入位置 ];
(1)在user表最后插入一列:
alter table user add test_column varchar(30);
(2)在user表中age后插入一列:
alter table user add birthday date after age;
(3)在已知表 user_role 中添加一列,默认值为1
alter table user_role add isDelete int not null default 1;
2、修改列
语法:alert table 表名 change 列名称 列新名称 新列数据类型
(1)在user表中age后插入一列:
alter table user change test_column new_test_column varchar(30);
3、删除列
语法:alert table 表名 drop 列名称;
(1) 删除user表中 new_test_column 这一列:
alter table user drop new_test_column;
4、重命名表
语法: alter table 表名 rename 新表名;
(1)把user 表名改为 userss
alter table user rename userss;
5、删除整张表和数据库
删除表:drop table 表名;
删除整个数据库:drop database 数据库名;
补充:
七、对表的复制、查询插入:
语法:create table 表 select查询语句; - 将查询结果当做表,创建出来。
语法:insert into 表 select查询语句; - 将查询结果插入到一张表中(2个表的字段和行数必须一样才行)。