SQL

1,order by:排序用的  比如:order by 'grade' '姓名' DESC(降序),默认升序 先按照'grade',然后就是'姓名'

2,就算select没有选择某些列,下面依旧可以用某些列去排序

3,limit:如果有10个用户数据,要显示第四个,第五个,则 limit 3,2(跳过前面三个,在选择前两个)

4,SQL分为内连接和外连接

     连接两个表,用join   

 select *

from customer c   --把c作为customer的缩写

join order on order.id =customer.id

跨数据连接:跟上面那个差不多,只是要加几个前缀

自连接:要给同个表赋予两个名字

use sql_h;
select e.employee_id,e.first_name,m.first_name as man
from employees e
join employees m on e.reports_to=m.employee_id

多个表格进行连接:运用多个join

复合连接:join后面的on可以用and去连接两个条件

内连接(inner join:其中inner可有可无)

外连接(outer join):分为左连接和右连接,left join(显示左表的所有数据) and right join(显示右表的所有数据)举例:如果一个商店的会员,有一些用户没有购买记录,但是要显示所有的会员,没有购买记录的话就用null,这时就用外连接

on e.customer=m.customer and e.id=m.id(相当于using(customer,id))

using只能用在不同表中相同的列

自然连接:natural join  虽说简单使用,但是有点危险,这是系统自己匹配的

交叉连接:cross join

union:合并多个查询

如果要给2019年的交易设置为'active',之前的交易设置为'unactive',代码为:

select order_id,order_data,'unactive' as status

from orders

where order_data<'2019-01-01',

union

select order_id,order_data,'active'

from orders

where order_data>='2019-01-01'

插入单列行:

1,insert into customers('姓名','积分')

values('gogo','10'),如果没有值就写default

插入多行:

1,insert into shape('name')

values('a') ('b')

LAST_INSERT_ID()  获取最后一个ID

创建新表并复制数据到表里去:

create table xinbiao as

--子查询

select *

from jiubiao

可以先把表里的数据删掉,然后用

insert into xinbiao #括号去掉

select *

from jiubiao

where time>'2019-01-01'

更新单行数据:

updata 表名

set 某一列='某某'

where id=3

更新多行:

updata i

set '姓名'=s

where id in (select c_id from client where home in ('us','ua')

删除几行:

delete from 表名

聚合函数:max() min() AVG() SUM() COUNT()

min('成绩'*1.1)

count(*)  不管是不是非空,都算下去

count(distinct id)  去掉重复项 

group by:分组  

select client_id,min(invoice_total)

from invoices

(where)

group by client_id

order by total_sales desc(降序)

 group by前不能用where,因为where的运行在group by前,运行时还没分组. 

所以用having,作用相当于where,一个在分组前,一个在分组后 

with rollup:只能用于聚合值的列

group by client_id with rollup 

编写复杂查询

可以在select   from   where中使用子查询

all(1,2,3)=3   any(1,2,3)=some(1,2,3)=1

相关子查询:

in和exist的区别:in是返回一个列表,当数据很多时,效率不高,而exist效率高

a-b as 'cha'--这样是不行的,a-(select b) as 'cha'

from的子查询后一定需要给个新的表名

内置函数:

round(5.73,1)=5.7

truncate(5.7351,2)=5.73 

rand()  生成0-1之间的随机小数

length('str')=3

upper() lower()

了解更多请查询 MySQL numeric functions

消除空格   LTRIM('sky     ')=sky RTRIM()   TRIM()也是一样

left('sdsd',2)=sd

substring('sdsda',2,4)=dsda

locate('n','asdnd')=4  无所谓大小写

concat('sds','ds')=sdsds

时间函数:now() curdata()   curtime()  year(now())   month(now())  day(now())

这些是返回整数值

返回字符串:select extract(year from now())

具体的时间函数,查:MySQL data format string

select data_add(now(),interval 1 year)在原本的基础上加上1年

select data_sub(now(),interval 1 year)

datadiff('2019-01-05','2019-01-07')=2天

time_to_sec('00:01')=60

IFNULL(s,'ok')  如果s上为空,则换为OK

if函数  IF(year='2019','1','2')  如果year=2019,那么返回1,用在select上

case运算符:和MATLAB中的switch....case差不多

case

when  year='2019'  then 1

when  year='2018'   then  2

else  '3'

end as cat

创建视图:创建后跟表格一样

select上   create view 名字  as+选择子句

更改或删除视图

删除:drop view+表名

create or replace view +表名 as+选择子句  这种不用删除掉视图

可更新视图:没有distinct   聚合函数   group by/having  union 这些语句,就为可更新视图

with check opion   防止update或者delete语句将行从视图中删除

                                    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值