MySQL---基本操作DQL(上)(基本查询语法,算术运算符,比较运算符,逻辑运算符,位运算符)

1. 基本查询语法

select  [all|distinct]

  <目标列的表达式1> [别名],

  <目标列的表达式2> [别名]...

from <表名或视图名> [别名],<表名或视图名> [别名]...

[where<条件表达式>]

[group by <列名>

[having <条件表达式>]]

[order by <列名> [asc|desc]]

[limit <数字或者列表>];

简化版:select *| 列名 from 表 where 条件

-- 创建数据库
create database if not exist mydb2;
use mydb2;
-- 创建商品表:
create table product(
 pid int primary key auto_increment, -- 商品编号
 pname varchar(20) not null , -- 商品名字
 price double,  -- 商品价格
 category_id varchar(20) -- 商品所属分类
);
insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲’,200,'c001');
insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲霸休闲裤',266,'c002');
insert into product values(null,'海澜之家卫衣',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');
 
insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');
 
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);

-- 1.查询所有的商品.  
select *  from product;
-- 2.查询商品名和商品价格. 
select pname,price from product;
-- 3.别名查询.使用的关键字是as(as可以省略的).  
-- 3.1表别名: 
select * from product as p;
-- 3.2列别名:
select pname as pn from product; 
-- 4.去掉重复值.  
select distinct price from product;
-- 5.查询结果是表达式(运算查询):将所有商品的价格+10元进行显示.
select pname,price+10 from product;

2. 运算符

算术运算符:

算术运算符

说明

+

加法运算

-

减法运算

*

乘法运算

/ 或  DIV

除法运算,返回商

% 或  MOD

求余运算,返回余数

select 6 + 2;
select 6 - 2;
select 6 * 2;
select 6 / 2;
select 6 % 2;
 
-- 将每件商品的价格加10
select name,price + 10 as new_price from product;
-- 将所有商品的价格上调10%
select pname,price * 1.1 as new_price from product;

比较运算符:

比较运算符

说明

=

等于

<        <=

小于和小于等于

>        >=

大于和大于等于

<=>

安全的等于,两个操作码均为NULL时,其所得值为1

而当一个操作码为NULL时,其所得值为0

<> !=

不等于

IS NULL  ISNULL

判断一个值是否为 NULL

IS NOT NULL

判断一个值是否不为 NULL

LEAST

当有两个或多个参数时,返回最小值

GREATEST

当有两个或多个参数时,返回最大值

BETWEEN AND

判断一个值是否落在两个值之间

IN

判断一个值是IN列表中的任意一个值

NOT IN

判断一个值不是IN列表中的任意一个值

LIKE

通配符匹配

REGEXP

正则表达式匹配

-- 查询商品名称为“海尔洗衣机”的商品所有信息:
select * from product where pname = '海尔洗衣机';
 
-- 查询价格为800商品
select * from product where price = 800;
 
-- 查询价格不是800的所有商品
select * from product where price != 800;
select * from product where price <> 800;
select * from product where not(price = 800);
 
-- 查询商品价格大于60元的所有商品信息
select * from product where price > 60;
 
 
-- 查询商品价格在200到1000之间所有商品
select * from product where price >= 200 and price <=1000;
select * from product where price between 200 and 1000;


-- 查询商品价格是200或800的所有商品
select * from product where price = 200 or price = 800;
select * from product where price in (200,800);
 
-- 查询含有‘裤'字的所有商品
select * from product where pname like ‘%裤%';
 
-- 查询以'海'开头的所有商品
select * from product where pname like '海%';
 
-- 查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%';
 
-- 查询category_id为null的商品
select * from product where category_id is null;
 
-- 查询category_id不为null分类的商品
select * from product where category_id is not null;
-- 使用least求最小值
select least(10, 20, 30); -- 10
select least(10, null , 30); -- null
 
-- 使用greatest求最大值
select greatest(10, 20, 30);
select greatest(10, null, 30); -- null

逻辑运算符:

逻辑运算符

说明

NOT 或者 !

逻辑非

AND 或者 &&

逻辑与

OR 或者 ||

逻辑或

XOR

逻辑异或

-- 查询商品价格是200或800的所有商品
select * from product where price in(200,800);
select * from product where price = 200 or price = 800;
select * from product where price = 200 || price = 800;

位运算符:

位运算符

说明

|

按位或

&

按位与

^

按位异或

<< 

按位左移

>> 

按位右移

~

按位取反,反转所有比特

注意:位运算符是在二进制数上进行计算的运算符。位运算会先将操作数变成二进制数,进行位运算。然后再将计算结果从二进制数变回十进制数。

select 3&5; -- 位与
select 3|5; -- 位或
select 3^5; -- 位异或
select 3>>1; -- 位左移
select 3<<1; -- 位右移
select ~3;   -- 位取反

3. 将一张表的数据导入到另一张表当中

INSERT INTO SELECT语句:

insert into Table2(field1,field2,…) select value1,value2,… from Table1

 或者:

insert into Table2 select * from Table1

要求目标表Table2必须存在

SELECT INTO FROM语句:

SELECT vale1, value2 into Table2 from Table1

 要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。

(日常美图时间)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三月七꧁ ꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值