数据库命令的编写方法—3

1.       b_student_score表为主,在后面加上b_subject_info表中的sub_name

select s.*,i.sub_name

from

b_student_score s left join b_subject_info i on i.id=s.sub_id

;

数据库命令的编写方法—3 - lee - 晓的博客

 

2.       把第一个插叙和第二个查询联合展示出来i

select

a.id,a.accont_name

from

b_user_info a

where a.class_id=1

union all

select

a.id,a.accont_name

from

b_user_info a

where a.class_id=2

;

数据库命令的编写方法—3 - lee - 晓的博客

 

注意:

  1. 两个查询结果字段个数必须相等
  2. 两个查询结果字段类型必须对应
  3. union all是全部合成,不会去掉重复
  4. union会去掉重复

 

3.       distinct去掉重复

select distinct a.stu_id

from b_student_score a

;

 

数据库命令的编写方法—3 - lee - 晓的博客

 

4.  查询表b_user_info中重复的class_id

select a.*

from

b_user_info a,

b_user_info b

where a.class_id=b.class_id and a.id<>b.id

;

运行方式:将表b_user_info当成两个表来查询,首先对比出class_id相等的部分,再对比出主键id不相等的部部分,去掉主键id向同的,取出主键id不同的,列出结果

 

数据库命令的编写方法—3 - lee - 晓的博客

 

5.       case  when  then  end 的用法

 

select a.id,a.class_id,

case a.class_id

when 1 then

'1'

when 2 then

'2'

else

'3'

end ccc

from

b_user_info a

;

 

数据库命令的编写方法—3 - lee - 晓的博客

 

select a.id,a.class_id,

case when a.class_id<>1 then

'不是1'

when a.class_id=2 then

'2'

else

'x'

end ccc

from

b_user_info a

;

 

数据库命令的编写方法—3 - lee - 晓的博客

 

case 后面跟条件,when后面也跟条件,进入相应的显示结果 ,但是不能同时使用两个条件

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值