mysql的数据库表的操作

数据库表的操作

1
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES  ( value1, value2,...valueN );

一次增加一条数据

1
insert into student(stuno,name,sex,age) values(100,'xyq',1,21);

一次增加多条数据

1
2
insert into student(stuno,name,sex,age) values(100,'xyq',1,21),(101,'hsz',0,20),(102,'csh',1,21);

1
DELETE FROM table_name [WHERE Clause]

1
2
3
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]

1
2
-- 将学号为100的用户名改为 谢永强
update student set name = '谢永强' where stuno = 100;

单表基本查询

1
2
3
4
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]

select

  • as: 查询结果s定个别名;
  • * : 查询所有列;
  • case: 对查询结果进一步处理
  • distinct:去重
1
select name,case ismale when 1 then '男' else '女' end as '性别', salary as '工资' from employee;

where

  • in 在某个范围中

order by :desc 降序 asc 降序

limit:m,n 跳过n条数据,取出m条数据

1
2
3
4
-- 查询工资最高的女员工

select * from employee where ismale = 0 order by salary desc limit 0,1;

联表查询

  • INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
  • LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
  • RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。