各类SQL语句汇总


1.将结果进行排序后,对排序后的数据进行排名

select 
t.score Score,
CAST((case when @rowtotal = t.score then @rownum:=@rownum + 0
      when @rowtotal := t.score then @rownum:=@rownum + 1
      when @rowtotal = 0 then @rownum:=@rownum+1 end) AS SIGNED) as `Rank` from 
(select score from scores order by score desc) t,
(select @rownum:=0,@rowtotal:=null)c

其中case when的语句解释如下:

  • 第一个when是判断当前的score是否等于当前赋值的@rowtotal,如果是,则排名不变(排名并列)
  • 一开始的时候,第一个when是不会执行的,因为此时@rowtotal为null,而到了第二个when的时候@rowtotal := score将score赋值给了@rowtotal,只要score>0,那么改判断都为true,就将@rownum行号+1。
  • 第三个when只有当第二个when将@rowtotal赋值为0的时候,才会执行。


2.删除语句联表查询

delete from table1 t1,table2 t2 where t1.id > t2.id and t1.email = t2.email;

该语句时错误的,执行后会报错,应该写成以下的方式

delete t1 from table1 t1,table2 t2 where t1.id > t2.id and t1.email = t2.email;

两张表关联查询的话,就需要制定好要删除哪张表。

后续有其他的SQL语句,再来继续添加
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值