三、表内容操作
1、增
语法:insert into 表 (列名,列名...) values (值,值,值...)
# 插入单条数据insert into 表 (列名,列名...) values (值,值,值...)# 插入多条数据insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)# 插入另一条语句的查询结果insert into 表 (列名,列名...) select 列名,列名... from 表
2、删
语法:delete from 表
delete from 表;delete from 表 where id=1;
3、改
语法:update 表 set name = 'nick' where id>1
update 表 set name = 'nick' where id>1
4、查
语法:
select * from 表
SELECT DISTINCT 列名称 FROM 表名称 ; (去重)
select * from 表select * from 表 where id > 1select nid,name,gender as gg from 表 where id > 1# as 做别名
5、条件
语法:select * from 表 where id > 1
select * from 表 where id > 1 and name != 'nick' and num = 12; # 多个条件select * from 表 where id between 5 and 16; # id在5到16之间select * from 表 where id in (11,22,33); # id在元祖中select * from 表 where id not in (11,22,33); # id不在元祖中select * from 表 where id in (select nid from 表); # id在查询结果中
6、通配符
语法:select * from 表 where name like '_n%'
select * from 表 where name like 'ni%' # ni开头的所有(多个字符串)select * from 表 where name like 's_' # s开头的所有(一个字符)
7、限制
语法:select * from 表 limit 9,5;
select * from 表 limit 5; # 前5行select * from 表 limit 9,5; # 从第9行开始的5行select * from 表 limit 5 offset 9 # 从第9行开始的5行
8、排序
语法:select * from 表 order by 列1 desc,列2 asc
select * from 表 order by 列 asc # 根据 “列” 从小到大排列select * from 表 order by 列 desc # 根据 “列” 从大到小排列select * from 表 order by 列1 desc,列2 asc # 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
9、分组
语法:select num from 表 group by num
select num from 表 group by num # 根据num分组select num,nid from 表 group by num,nid # 根据num和nid分组select num,nid from 表 where nid > 10 group by num,nid order nid descselect num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid # 内置函数select num from 表 group by num having max(id) > 10 # 前面计算的结果交由后面处理注:group by 必须在where之后,order by之前
count(*)、count(1) # 表示个数sum(score) # 表示和max(score) # 表示最大数min(score) # 表示最小数having # 要用前面处理结果是用having。
10、连表
语法:inner join . on、left join . on、right join . on
无对应关系则不显示select A.num, A.name, B.namefrom A,BWhere A.nid = B.nid无对应关系则不显示select A.num, A.name, B.namefrom A inner join Bon A.nid = B.nidA表所有显示,如果B中无对应关系,则值为nullselect A.num, A.name, B.namefrom A left join Bon A.nid = B.nidB表所有显示,如果B中无对应关系,则值为nullselect A.num, A.name, B.namefrom A right join Bon A.nid = B.nid
11、组合
语法:union、union all
组合,自动处理重合select nicknamefrom Aunionselect namefrom B组合,不处理重合select nicknamefrom Aunion allselect namefrom B
查询表数据
1> 命令语法:select<字段1,字段2,…>from<表名>where<表达式>2> 查询所有mysql> select * from student;+----+-----------------+------+--------+| id | name | age | dept |+----+-----------------+-------+--------+| 2 | oldsuo | 0 | NULL || 3 | kangknag | 0 | NULL || 4 | kangkang | 0 | NULL |+----+-----------------+-------+--------+3 rows in set (0.00 sec)3> 查询某列。不用*,查询的列列出来。mysql> select id,name from student;+----+----------------+| id | name |+----+----------------+| 2 | oldsuo || 3 | kangknag || 4 | kangkang |+----+-----------------+3 rows in set (0.00 sec)4> 指定条件查询mysql> select id,name from student where name='oldsuo' and id=2;+----+--------------+| id | name |+----+---------------+| 2 | oldsuo |+----+----------------+1 row in set (0.00 sec)mysql> select id,name from student where name='oldsuo' or id=3;+----+----------+| id | name |+----+----------+| 2 | oldsuo || 3 | kangknag |+----+----------+2 rows in set (0.03 sec)#升序mysql> select id,name from student order by id asc;+----+----------+| id | name |+----+----------+| 2 | oldsuo || 3 | kangknag || 4 | kangkang |+----+----------+3 rows in set (0.00 sec)#倒叙mysql> select id,name from student order by id desc;+----+----------+| id | name |+----+----------+| 4 | kangkang || 3 | kangknag || 2 | oldsuo |+----+----------+3 rows in set (0.00 sec)
表中插入数据
1> 插入单个数据,student为表的名称。mysql> insert into student(id,name) values(1,'nick');Query OK, 1 row affected (0.02 sec)mysql> select * from student;+----+--------------+------+--------+| id | name | age | dept |+----+--------------+------+--------+| 1 | nick | 0 | NULL |+----+--------------+------+---------+1 row in set (0.00 sec)3> 批量插入数据,student为表的名称。mysql> INSERT INTO student(id,name) values(2,'oldsuo'),(3,'kangknag'),(4,'kangkang');Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: 0mysql> select * from student;+----+-----------------+------+------+| id | name | age | dept |+----+-----------------+------+------+| 1 | nick | 0 | NULL || 2 | oldsuo | 0 | NULL || 3 | kangknag | 0 | NULL || 4 | kangknag | 0 | NULL |+----+-----------------+------+--------+3 rows in set (0.00 sec)
表中删除数据
1> 删除所有数据,student为表的名称。mysql> delete from student;Query OK, 8 rows affected (0.01 sec)mysql> select * from student;Empty set (0.00 sec)2> 删除表中的某行或某些mysql> delete from student where id=4;Query OK, 1 row affected (0.00 sec)mysql> delete from student where id>2;Query OK, 1 row affected (0.00 sec)mysql> delete from student where name='oldsuo';Query OK, 1 row affected (0.00 sec)3> 直接清空某张表mysql> truncate table student;Query OK, 0 rows affected (0.00 sec)
四、其它命令
1、查看建表语句
show create table 表名\G
mysql> use nick_defailtDatabase changedmysql> create table student (id int(4) not null,name char(20) not null,age tinyint(2) NOT NULL default '0',dept varchar(16) default NULL);Query OK, 0 rows affected (0.05 sec)mysql> show create table student\G #查看建表语句*************************** 1. row ***************************Table: studentCreate Table: CREATE TABLE `student` ( #CREATE TABLE创建表的固定关键字,student为表名。`id` int(4) NOT NULL, #学号列,数字类型,长度为4,不为空值。`name` char(20) NOT NULL, #名字列,定长字符类型,长度20,不为空值。`age` tinyint(2) NOT NULL DEFAULT '0', #年龄列,很小的数字类型,长度为2不为空,默认0。`dept` varchar(16) DEFAULT NULL #系别列,变长字符类型,长度16,默认为空。) ENGINE=MyISAM DEFAULT CHARSET=latin1 #引擎和字符集,引擎默认InnoDB,字符集lantin1。1 row in set (0.00 sec)
2、查看表结构
desc 表名;
mysql> desc student;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int(4) | NO | | NULL | || name | char(20) | NO | | NULL | || age | tinyint(2) | NO | | 0 | || dept | varchar(16) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+4 rows in set (0.04 sec)
3、查看是否走索引
explain select * from 表名 where name ='nick' \G
用此命令查看是否sql语句是否还有优化的余地
mysql> explain select * from student where name ='student' \G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: studenttype: ref #有possible_keys: index_namekey: index_name #表示有,详细见下文key_len: 20ref: constrows: 1 #检索了几行Extra: Using where1 row in set (0.00 sec)
4、查询时间戳和日期的转换
MySQL提供了两个函数:from_unixtime(time_stamp) -> 将时间戳转换为日期unix_timestamp(date) -> 将指定的日期或者日期字符串转换为时间戳SELECT from_unixtime(duedate/1000) duedate FROM organ WHERE refer='suoning';
文章转载自Linux Windows server学习交流,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




