一直没有对SQL语句有一个系统化的学习整理近期整理了下。

一直没有对SQL语句有一个系统化的学习及整理,近期开源达人整理了下SQL,相对来说比较全面系统化,希望对大家有所帮助!!!

tb_list 表
tb_name 列

//==简单查询

2.1 select 语句基本结构

SELECT tb_list
INTO
FROM
WHERE
GROUP BY
HAVING
ORDER BY ASC | DESC

select tb_list
into
from
where
group by
having
order by asc|desc

2.2 单列查询
SELECT tb_list FROM tb_name;

2.3 多列查询
SELECT tb_name1,tb_name2, tb_name3 FROM tb_list;

2.4 查询所有列
SELECT * FROM tb_list;
select * form tb_list;

2.5 别名查询
SELECT tb_name AS “商品名称” FROM tb_list;

2.6 删除重复数据 不真实删除数据
distinct
select user_id,DISTINCT

2.7 限制查询结果

SQL:
SELECT TOP5 tb_name1,tb_name2 FROM tb_list;

MYSQL:
SELECT tb_name1,tb_name2 FROM tb_list LIMIT 5;

//===========================================

//==计算列查询

3.1 连接列值
select name + age as “名年纪” from tb_list;

3.2 查询中使用计算列
SELECT tb_id as 商品id, tb_name as 商品名, (tb_pric – tb_prec1 ) as 售价 FROM tb_list;
SELECT tb_id as 商品id, tb_name as 商品名, (tb_pric * tb_prec1 ) as 售价 FROM tb_list;
SELECT tb_id as 商品id, tb_name as 商品名, (tb_pric * tb_prec1 ) as 售价 FROM tb_list;
(tb_pric * tb_prec1 – tb_pric * tb_prec1)/ tb_sum as 售价 FROM tb_list WHERE tb_ss <> 0;

3.2 查询中使用表达式 (加文字,加数值,加新列和值)
select tb_name1 as 商品1,tb_name2 as 商品2, tb_pri + 50 as 进价+50 FROM tb_list;

//===========================================

//==条件查询

4.1 where 子句条件查询
select 字段 from 表名 where 条件
条件:= > < >= <= !< !> <>或!=

select * from tb_list where tb_id = 101;

//===========================================

//==范围查询

5.1查询2个值之间的数据

select tb_name1 as ‘名’, tb_name2 as ‘钱’
from tb_list
where tb_price between 1000 and 300;

5.2查询2个日期之前的数据
select tb_name1 as ‘名’, tb_name2 as ‘钱’
from tb_list
where TIME
between ‘2024-12-1’ and ‘2025-12-1’;

5.3between 中使用日期函数。
SELECT GETDATE();

select tb_name1 as ‘名’, tb_name2 as ‘钱’
from tb_list
where tb_time
between DATEADD(DAY,-1GETDATE())
and
GETDATE();

5.4查询不在两个数之间的数据
select tb_name1,tb_name2,tb_name3
from tb_list
where tb_id
not between 100 and 1000;

5.5日期时间查询

5.5.1 转换日期格式
1,把长日期格式转换为短日期的格式
2,将日期格式中的 – 转换为 /

5.5.2计算2个日期的间隔天数
1,DAY()函数
2,MONTH()函数
3,YEAR()函数

5.5.3 按指定日期查询数据

发表评论

邮箱地址不会被公开。