美文网首页
复习sql基本

复习sql基本

作者: 如果俞天阳会飞 | 来源:发表于2019-08-10 17:23 被阅读0次

来源菜鸟教程

基础

1.查询所有结果集

select * from product  // 返回值 int 类型 多少行

1.查询某一列

select  name from product  //查询产品name 列

SELECT DISTINCT 语句

在表中,一个列可能会包含多个重复值,有时希望列出不同(distinct)的值 distinct 关键字返回唯一不同的值


image.png
select distinct county from websites
image.png

where

select * from websites where id = 1;
image.png

WHERE 子句中的运算符

=   等于
<>  不等于。注释:在 SQL 的一些版本中,该操作符可被写成 !=
>   大于
<   小于
>=  大于等于
<=  小于等于
BETWEEN 在某个范围内
LIKE    搜索某种模式
IN  指定针对某个列的多个可能值

<>不等于

select * from product where price<>5000; 查询价格不等于 5000
image.png

between 在某个范围

查询产品 价格 200 到 800之间的 包括 200 和 800
select * from product where price between 200 and 800;
image.png

like 模糊查询

-- 查询 uname 中包含 手机的两个字
select * from product where uname like '%手机%';
select * from product where uname like '%手机'; // 末尾带有手机的

in 指定某个或多个值

 select * from product where price in(200, 800);
image.png

not in 和in相反

not in 和 in 相反
 select * from product where price not in(200, 800);
image.png

sql and 和 or 运算符

如果第一个条件和第二个条件都成立,则 and 运算符显示一条记录
如果第一个条件和第二个条件只有一个成立 则or运算符显示一条记录

and 运算符

select * from product where category_id = 'c003' and price >= 800;
category_id = 'c003' 并且价格 大于等于800;
image.png

or 运算方法

category_id = c003 的 或者价格大于 3000的
select * from product where category_id = 'c003' or price > 3000;
image.png

结合 and 和 or

select * from product where price > 500 and(category_id = 'c001' or category_id = 'c002');
价格大于500 并且 category_id 等于 c001 或者 category_id 等于 c002
image.png

sql order by (对结果集进行排序)

对上面的查询结果进行排序
select * from product where price > 500 and(category_id = 'c001' or category_id = 'c002') order by price;
image.png

order by 关键字对结果集按照一个列或者多个列进行排序
order by 关键字默认按照升序对记录进行排序,如果需要按照降序对记录进行排序,使用 DESC关键字

desc 关键字 降序

 select * from product order by price desc;
image.png

order by 多列

select * from product order by category_id, price;
image.png

order by 多列的时候,先按照第一个 column name 排序 在按照第二个column排序,
1.先按照 category_id 值这一列排序,
2.然后再按照price排序
3.order by 排序时,不写明 asc desc 的时候 默认 asc

order by A,B 这个时候都是默认按升序排列
order by A desc,B 这个时候 A 降序,B 升序排列
order by A ,B desc 这个时候 A 升序,B 降序排列
既desc 或者asc只对它紧跟着的第一个列名有效,其他不受影响,仍然是默认的升序

例子

select * from product order by category_id desc, price asc;
首先对category_id 降序排序,, 然后 price 升序
image.png

insert into

INSERT INTO 语句用于向表中插入新记录。
第一种形式无需指定要插入数据的列名,只需提供被插入的值即可:

INSERT INTO table_name
VALUES (value1,value2,value3,...);

第二种形式需要指定列名及被插入的值:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

例子:

insert into hema (id, name, price) values(1, 'xuebi', 5);

 insert into hema values(2, 'kele',6);
image.png

update

update 用于更新表中存在的记录

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

例子


image.png
更新xuebi 价格
update hema set price = 8 where name = 'xuebi';
image.png

警告
在更新记录时要格外小心,如果我们省略了 WHERE 子句将表中的所有价格都改成 8
执行没有 WHERE 子句的 UPDATE 要慎重,再慎重

在 MySQL 中可以通过设置 sql_safe_updates 这个自带的参数来解决,当该参数开启的情况下,你必须在update 语句后携带 where 条件,否则就会报错。
set sql_safe_updates=1; 表示开启该参数 set sql_safe_updates=0;停止

delete

delete语句用于删除表中的记录

SQL DELETE 语法

DELETE FROM table_name
WHERE some_column=some_value;

注意 delete 语句如果 省略where 将表中所有的记录都删除!

例子


image.png
delete from hema where name = 'xuebi';
image.png

删除所有数据

你可以在不删除表的情况下,删除表中所有的行,这意味着表结构,属性,索引保持不变

delete from table_name;
或
delete * from table_name;

在删除记录时要格外小心!因为你不能重来!

相关文章

  • 复习sql基本

    来源菜鸟教程 基础 1.查询所有结果集 1.查询某一列 SELECT DISTINCT 语句 在表中,一个列可能会...

  • 复习SQL

    参考自:runoob Basis Basic Usage Where Distinct Choose the on...

  • SQL复习

    东西学了不用真是看完就忘 有空要多练习才行 老忘好浪费 基本: select:获取数据 select A from...

  • SQL语句基本使用

    SQL语句基本使用——增删改查 SQL语句基本使用——WHERE子句 SQL语句基本使用——AND和OR的使用 S...

  • SQL基础语法笔记教程整理

    最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下思路,以备日后复习之用。 PS:本文...

  • SQL知识复习

    三大重要操作 scanning sorting hashing UNION,EXCEPT, INTERSECT相当...

  • SQL复习-ddl

    DDL 创造,毁灭,更改 CREATE TABLE Branch(branch_id VARCHAR(15), n...

  • 复习sql(三)

    sql别名 通过使用 SQL,可以为表名称或列名称指定别名。通过使用sql,可以为表名称或列名称指定别名基本上,创...

  • 复习sql(一)

    启动 net start mysql停止 net stop mysql 登陆 mysql -u root -p密码...

  • SQL复习-dml

    选取FROM SELECT name FROM Customer; //数字可以用四则运算SELECT * FRO...

网友评论

      本文标题:复习sql基本

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