美文网首页
SQL语句基础

SQL语句基础

作者: 堆石成山 | 来源:发表于2020-09-01 20:45 被阅读0次

一、SQL 基础语句(大小写功能一致):
§1 SQL select语句:从表中选取数据
★ select * from 表——从表中选择所有列的数据
★ select 列 from 表——从表中选择某列的数据
★ select 列1,列2 from 表——从表中选择列1,列2的数据

§2 SQL select distinct语句:从表中选择不同的数据
★ select distinct 列 from 表——从表中选择列的不同数据

§3 SQL where语句:从表中选择有条件的数据
★ select 列 from 表 where 列 运算符 值——从表中选择有条件的数据
运算符:=、<>、>、<、>=、<=、BETWEEN、LIKE
值:文本值要用‘值’

§4 SQL and和or语句:从表中选择有一个以上条件的数据
★ select 列 from 表 where 列 运算符 值 and/or 列 运算符 值——从表中选择有两个条件的数据

§5 SQL order by语句:从表中选择的列对结果集进行排序(DESC降序ASC升序)
★ select 列 from 表 order by列——从表中选择列,并排序(升序)
★ select 列1 from 表 order by列2 desc——从表中选择列1,并安列2排序(降序)

§6 SQL insert语句:向表中插入新的行
★ insert into 表 values (值1,值2...)——在表中插入一行值
★ insert into 表 (列1,列2...) values(值1,值2...)——在表中插入对应列的值

§7 SQL update语句:更新值
★ update 表 set 列=新值 where 列=某值——更新值
★ update 表 set 列1=新值1, 列2=新值2 where列=某值——更新值

§8 SQL delete语句:删除值
★ delect from 表 where 列=某值——删除值
★ delect from 表——删除表中所有行

二、下面是一个名为"Students"的表:

  • Id - Name - Gender -Chinese - Mathematics - English - Comprehensive - TotalScore -
  • 1 - Lucy - Girl - 131 - 149 - 139 - 270 - 689 -
  • 2 - Tony - Boy - 128 - 150 - 135 - 290 - 703 -
  • 3 - Lily - Girl - 135 - 143 - 142 - 281 - 701 -

1、从表中选出“Comprehensive”列的数据

select Comprehensive from Students

2、从表中选出“Comprehensive”和“TotalScore”两列的数据

select Comprehensive,TotalScore from Students

3、从表中选出所有列的数据

select * from Students

4、从表中选出“Gender”列不重复的数据

select distinct Gender from Students

5、从表中选出Mathematics等于满分150的数据

select * from Students where Mathematics=150

6、从表中选出Name等于Lily的数据

select * from Students where Name='Lily'

以上两条注意文本值和数值的条件区别
7、从表中选出总分大于700且为女孩的数据

select * from Students where Gender='Girl' and TotalScore>700

8、从表中选出数学为150或者性别为女孩的数据

select * from Students where Gender='Girl' or Mathematics=700

9、以总分从高到低排序

select * from Students order by TotalScore desc

9、以性别排序和总分从高到低排序

select * from Students order by Gender desc,TotalScore desc

desc降序排列、asc顺序排列(单个顺序时可不写)
10、向表中插入一行数据

insert into Students values (4,'Hui','Girl',139,146,148,291,724)

11、向表中指定列插入数据

insert into Students (Name,Gender) values ('Tom',Boy')

12、修改表中某行的若干列

update Students set Chinese=140,TotalScore=710 where Name='Tom'

13、删除名为Tom这行

delete from Students where Name='Tom'

14、删除所有行

delete from Students

相关文章

  • 每天一SQL语句(01):SQL 语句基础篇

    【开篇】SQL 语句基础篇 【前言】SQL 语法 1、SQL 注意事项 (1)SQL语句对大小写不敏感。 (2) ...

  • MySQL

    数据类型 sql基础 数据库表 SQL SELECT 语句: SQL WHERE 子句: SQL AND & OR...

  • SQL基础及元数据获取(数据类型,表的属性)

    1、SQL基础应用 ①.SQL的介绍SQL标准:SQL-92、SQL-99SQL_MODE:都是为了保证SQL语句...

  • SQL语句

    SQL基础应用 SQL语句自动补全 SQL的介绍 SQL-92标准SQL-99标准 image SQL常用分类 表...

  • MYSQL SQL 语句

    基础sql语句 库管理 表管理 用户管理:

  • SQL语句基础

    一、SQL 基础语句(大小写功能一致):§1 SQL select语句:从表中选取数据★ select * fro...

  • 深入浅出MySQL读书笔记

    SQL基础 SQL分类 SQL语句主要可以划分为以下3个类别。 DDL (Data Definition Lang...

  • SQL基础语句

    SQL 基本语句记录 新建表 删除表: 插入数据: 删除数据: 更新数据: 新增字段: 删除字段: 修改字段: 重...

  • SQL基础语句

    select distinct [字段名] from [表名];用于查询该列不重复的所有值。 where子句中可以...

  • SQL基础语句

    查询:select * from table_name where 条件 查询的结果集中使用别名:select 列...

网友评论

      本文标题:SQL语句基础

      本文链接:https://www.haomeiwen.com/subject/uhppsktx.html