SQL二刷(语法)

2023-02-05  本文已影响0人  山猪打不过家猪

1.inner join 和 left join 区别

image.png

总结:

select count(t0.user_id) as base_cnt, count(t1.user_id) as rentention_cnt, count(t1/user_id)/count(t0.user_id) as retention_rate_id
(select dt,user_id from oder_data.orders where dt = '20221202' group by dt,user_id) t0 left join (select dt,user_id from order_data.orders where dt = '20221203' group by dt,user_id) t1 on t0.user_id = t1.user_id

2. 索引设计原则

2.1什么情况需要建立索引
2.2 创建索引
image.png
create index i_account_outAmount on Account(OutAmount)

创建后500w数据的查询语句,从9秒变成0.1秒


image.png
image.png

3. 事务

pass

4. T-SQL编程

4.1 变量和赋值
image.png
#定义
declare @name varchar(20), @age int
#赋值方法1
set @name='fxx'
set @age=18
#赋值方法2
select @age=18,@name = 'fxx'
##输出
select @age as fxx_age,@name as fxx_name
4.2 逻辑控制
#交换两个数的值
declare @x int,@y int,@z int 
set @x = 3
set @y =100
begin 
    set @z = @x
    set @x = @y
    set @y = @z
end
select @x x,@y y
if (select count(*) from score) >0
begin
    print '考试人数大于0'
end
上一篇 下一篇

猜你喜欢

热点阅读