mysql 的基本语句
sql 语句的基础操作
简单的查询语句 select
select * from table (表名字, 自己创建的)
select 字段名1 , 字段名2 from table
select 字段名1 as '新名字' from table
select 字段名1 '新名字1' , 字段名2 '新名字2' from table
插入语句 insert into
insert into table ( 字段名1 , 字段名2 , . . . ) values ( 值1 , 值2 , . . . )
insert into table ( 字段名1 , 字段名2 , . . . ) values ( 值1 , 值2 , . . . ) , ( 值1 , 值2 , . . . ) , ( 值1 , 值2 , . . . ) . . . .
删除数据 delete
delete from table
delete from table where 条件
修改数据 update
update table set 字段名= 新值
update table set 字段名= 新值 where 条件
sql语句的条件语句 where
where 后边是详细的条件 可以是关系运算符 : > < >= <= = != <>(不等)
select * from table where 字段名 条件判断
条件语句可以使用 and 和 or 和 in() 进行多个条件查询操作
select * from table where 字段名1 条件 and 字段名2 条件;
select * from table where 字段名1 条件 or 字段名2 条件;
select * from table where 字段名 in ( 值1 , 值2 , . . . )
聚合函数 avg() 平均数 max() 最大值 min() 最小值 sum() 求和 count() 计数(统计)
select max ( 字段名) as '别名' , min ( 字段名) as '别名' , sum ( 字段名) '别名' , avg ( 字段名) '别名' from score
关键字查询 (模糊查询) like
select * from table where 字段名 like '%keywords%'
select * from table where 字段名 like '_keywords___'
分页查询 limit
select * from table limit length;
select * from table limit start , length;
分组 group by
select 字段名 from table group by 字段名;
select 字段名, count ( 字段名) from table group by 字段名;
排序 order by
select 字段名 from table order by 字段名 desc / asc
sql 的多表查询
给表起别名
select 表1 的别名. 字段名1 , 表2 的别名. 字段名1 from table1 别名, table2 别名 where 条件
把一个表查询的结果作为另一个表查询的条件
select grade from score where stu_id = ( select id from student where name = '李四' ) and c_name = '计算机'
针对上边的问题 另一中解决方法 : 等值连接
select b. name, a. c_name, a. grade from score a, student b where a. stu_id = b. id and b. name = '李四' and a. c_name = '计算机'