SQL查询语句

create table key1( --定义两个表
Kno int primary key,
Ksex char(5))

create table Key2(
Kmo int primary key,
Kno int,
constraint K_k foreign key(Kno) references key1)

insert  --插入数据
into key1
values(6,'a')

insert--插入数据
into key2
values(4,7)

insert  --插入数据
into key1
values(6,'a')

select* --*表示搜全部
from key1

select Kno--搜一个属性
from key1

select Kno,3-Kno--表示条件
from key1

select Kno,'fuck' title--可以自己编一个属性,还可以自己编一个属性列
from key1

select Kno,upper(Ksex) sss--控制输出格式
from key1

select distinct Ksex--去掉重复
from key1

select Kno--添加where 条件
from key1
where Ksex='be'

select Ksex--范围条件
from key1
where Kno<=3 and Kno>1--不可以用&&

select Ksex--范围条件
from key1
where Kno<=3 and Kno>1--不可以用&&

select Kno--按照多值查询的另一个表达方式
from key1
where Kno in(1,2)

select Kno--like查询
from key1
where Ksex like 'b%a'--可以有很多的组合,'b_a','b_a_','b\_'escape'\'可以转义'\'

select Kno--查找空的,is null不可以用=
from key1
where Kno is null

select Kno--降序desc 升序asc Descending  ascending
from key1
where Ksex='be'
order by Kno asc

select count(*)--计算有多少列
from key1

select count(Kno)--有条件的count
from key1
where Ksex like'b%'

select avg(Kno)--ava算平均数
from key1
where Ksex like 'ba'

select sum(Kno)--sum算Kno值加起来总数,还有max,min
from key1
where Ksex like 'b_'

select Ksex,count(Ksex) KK--group by的测试
from key1
--where count(Ksex)>1  后面不可以跟聚函数
group by Ksex--就没有重复的组
having count(Ksex)>1   --后面可以写 聚函数(相当于分组后的where

select key1.*,key2.*--链接两个数据库
from key1,key2
where key1.Kno=key2.Kno

select key1.*,key2.*
from key1
left join key2 on  --左(右)链接,以左(右)为基础,右(左)选择适合的数据
key1.Kno=key2.Kno  -- 没有where

select Ksex   --嵌套查询
from key1
where Kno in(--子查询语句不能用order by
    select Kno
    from key2
    where Kmo>=2)

select Ksex  --嵌套查询
from key1
where Kno=(  --可以用比较运算符,当然用=只有在子查询结果只有一个的时候
    select Kno
    from key2
    where Kmo=2)

select Ksex  --嵌套查询
from key1
where Kno>(  
    select max(Kno) --这里可以用集函数控制
    from key2
    where Kmo>=2)

select Ksex --嵌套查询
from key1
where exists(    --存在,子条件成立返回一个true
    select Kno
    from key2
    where Kno=2)

 

转载于:https://www.cnblogs.com/vhyc/p/5914396.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值