mysql实用篇

创建一个基本的表

create table student(id int not null,name char(10));

创建表之前选择在哪个数据库下面建表

use test

在新建的表里面插入一条数据

insert  into student  values(1,"李木")

修改数据

update  student set name="看个屁" where id=2

基本查询语法

select * from student where id=1

别名字段
别名字段会吧查询结果变为别名,实用
在这里插入图片描述
别名表

select id,name as "姓名" from student as s

排序
asc表示字段按升序排序;desc表示字段按降序排序。其中asc为默认值,排序不需要where

select * from student order by id desc

条件查询
id在0-1之前,包含0和1

select * from student where id  between 0 and 1
select * from student where id  between 1 and 2 and name="李木"

模糊查询
查询字段字符中间有“个”字的数据

select * from student where name like "%个%"

查询字段末尾有‘屁’字的数据

select * from student where name like "%屁"

查询字段以‘李’开头的数据

select * from student where name like "李%"

多表查询
交叉连接

select * from table1 ,table2 

左连接
左连接是已左边表中的数据为基准,若左表有数据右表没有数据,则显示左表中的数据右表中的数据显示为空。

右连接是左向外连接的反向连接,将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。

select * from tb_student as st left join tb_score as sc on st.studentNo=so.studentNo;

右连接

select * from tb_student as st right join tb_score as sc on st.studentNo=so.studentNo;

猜你喜欢

转载自blog.csdn.net/aaaaaaaaanjjj/article/details/128955480