MySql超级好用教程

文章详细介绍了SQL中的select查询语句,包括基本查询、多表关联查询(左外连接、右外连接、内连接、全连接)以及子查询的使用。同时,还提到了查询执行顺序和如何删除重复行数据的方法。

1、select查询语句

使用select语句查询数据,其通用语法:

Select columnName1,columnName2,... #columnName所需查询列名 使用星号(*)来代替所有字段
From  tableName #从数据表tableName查询数据
[WHERE Clause] #[]是可选项 where包含任何条件
[LIMIT N][ OFFSET M] #limit设定返回的记录数 offset开始查询的数据偏移量

实例所示:
在这里插入图片描述
select多表关联查询:

#左外连接查询
#左表tableName1 右表tableName2  左表重命名a 右表重命名b on包含两个表关联条件
select a.column1,b.column2 from tableName1 a left join tableName2 b on a.id = b.id;
#右外连接查询
select a.column1,b.column2 from tableName1 a right join tableName2 b on a.id = b.id;
#内连接查询
select a.column1,b.column2 from tableName1 a inner join tableName2 b on a.id = b.id;
#全连接查询
select a.column1,b.column2 from tableName1 a full join tableName2 b on a.id = b.id;
#左连接查询
select a.column1,b.column2 from tableName1 a left join tableName2 b on a.id = b.id where b.id is null;
#右连接查询
select a.column1,b.column2 from tableName1 a right join tableName2 b on a.id = b.id where a.id is null;
#两表独有数据集查询
select a.column1,b.column2 from tableName1 a full join tableName2 b on a.id = b.id where a.id is null or b.id is null;

如下图所示:
在这里插入图片描述
select子查询:

#查询tableNmae2的id值作为tableName1 的id条件值
select column1,column2 from tableName1 where id in (
select id from tableNmae2 where id between  3  and  10
)

select语句查询执行顺序:

select a.column1,a.column2... 
from tableName1 a left join tableName2 b on a.id = b.id 
where a.column3 = 'hello' 
group by a.column1,a.column2... 
having sum(a.column3) >1000
order by a.column1 desc
limit 10 offset 2
#sql语句查询执行顺序如下:
#1、执行from tableName1 
#2、执行on a.id = b.id 得到它们的交集
#3、执行left join tableName2
#4、执行where a.column3 = 'hello'判断条件
#5、执行group by a.column1,a.column2...进行分组 
#6、执行having sum(a.column3) >1000对结果再次进行筛选
#7、执行select a.column1,a.column2... 
#8、执行order by a.column1 desc进行降序排序
#9、limit 10 offset 2

删除重复行数据

DELETE a FROM wangdiantong_ordersale_detail a  
INNER JOIN (  
    SELECT stockout_id,goods_count,goods_num,goods_name,spec_name,paid,modified as min_id  
    FROM wangdiantong_ordersale_detail  
    GROUP BY stockout_id,goods_count,goods_num,goods_name,spec_name,paid,modified
    HAVING COUNT(*) > 1  
) b ON a.stockout_id = b.stockout_id and a.goods_count = b.goods_count and a.goods_num = b.goods_num and a.goods_name = b.goods_name


WHERE t1.id <> t2.min_id;

CTE临时结果集

WITH temps as (
	SELECT attend_name FROM attend_info limit 1
)
SELECT temps.attend_name from temps

with recursive temp as(
	SELECT 1 as n
	UNION ALL
	SELECT n + 1 FROM temp WHERE n <6
)
SELECT *FROM temp

关注我,不断为您更新精彩内容,敬请期待…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MtoSlc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值