mysql sql語句積累
mysql語句積累
1、建立資料庫、檢視資料庫、切換資料庫
create database dbname;
show databases;
use dbname;
2、刪除資料庫
drop database dbname;
3、建立表
create table student ( sno varchar(20) not null primary key, sname varchar(20) not null, ssex varchar(20) not null, sbirthday datetime, class varchar(20)
根據已有的表建立新表:
A:create
table
tab_new
like
tab_old (使用舊錶建立新表)
B:
create
table
tab_new
as
select
col1,col2…
from
tab_old definition
only
4、刪除新表
drop
table
tabname
5、增加一個列、修改列
、刪除列
alter
table
tabname
add
column
col type
例:alter table user(表名) add column name(新增欄位名) varchar(20) null default null comment '姓名' after age(欄位名);
例:alter table user(表名) modify column name(欄位名) varchar(50) not NULL default ‘’ comment '姓名' after age(欄位名);
例:alter table user(表名) drop column name(欄位名) ;
6、新增主鍵、刪除主鍵
alter
table
tabname
add
primary
key
(col);
alter
table
tabname
drop
primary
key
(col);
7、建立索引、刪除索引
create
[
unique
]
index
idxname
on
tabname(col….)
drop
index
idxname
8、建立檢視、刪除檢視
create
view
viewname
as
select
statement
drop
view
viewname
9、基本sql語句
選擇:
select
*
from
table1
where
範圍
插入:
insert
into
table1(field1,field2)
values
(value1,value2)
刪除:
delete
from
table1
where
範圍
更新:
update
table1
set
field1=value1
where
範圍
查詢:
select
*
from
table1
where
field1
like
’%value1%’
---like的語法很精妙,查資料!
排序:
select
*
from
table1
order
by
field1,field2 [
desc
]
總數:
select
count
as
totalcount
from
table1
求和:
select
sum
(field1)
as
sumvalue
from
table1
平均:
select
avg
(field1)
as
avgvalue
from
table1
最大:
select
max
(field1)
as
maxvalue
from
table1
最小:
select
min
(field1)
as
minvalue
from
table1
分頁:select * from table limit (pageNo-1)*pageSize, pageSize;
10、使用外連線
A、
left
(
outer
)
join
:
左外連線(左連線):結果集幾包括連線表的匹配行,也包括左連線表的所有行。
select
a.a, a.b, a.c, b.c, b.d, b.f
from
a
LEFT
JOIN
b
ON
a.a = b.c
B:
right
(
outer
)
join
:
右外連線(右連線):結果集既包括連線表的匹配連線行,也包括右連線表的所有行。
C:
full
/
cross
(
outer
)
join
:
全外連線:不僅包括符號連線表的匹配行,還包括兩個連線表中的所有記錄。
11、資料庫去重
刪除表中多餘的重複記錄(多個欄位),只留有rowid最小的記錄
DELETE
FROM
user u
WHERE
(u.peopleId, u.seq) IN (
SELECT
peopleId,
seq
FROM
user
GROUP BY
peopleId,
seq
HAVING
count(*) > 1
)
AND id NOT IN (
SELECT
min(id)
FROM
user
GROUP BY
peopleId,
seq
HAVING
count(*) > 1
)
11、統計訂單近七天的資料
select a.click_date as date,ifnull(b.amount,0) as amount
from (
SELECT curdate() as click_date
union all
SELECT date_sub(curdate(), interval 1 day) as click_date
union all
SELECT date_sub(curdate(), interval 2 day) as click_date
union all
SELECT date_sub(curdate(), interval 3 day) as click_date
union all
SELECT date_sub(curdate(), interval 4 day) as click_date
union all
SELECT date_sub(curdate(), interval 5 day) as click_date
union all
SELECT date_sub(curdate(), interval 6 day) as click_date
) a left join (
SELECT DATE_FORMAT(create_time, '%Y-%m-%d') as date ,sum(amount) as amount
FROM order
GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d')
) b on a.click_date = b.date ORDER BY a.click_date
12、統計購買數量在一定區間的人數
SELECT
elt( INTERVAL ( t.buyCount, 0, 10, 30, 90 ), '0~10', '10~30', '30~90','90+' ) AS device,
count( t.user_id ) AS count
FROM
( SELECT user_id, sum( goods_count ) AS buyCount FROM order_detail GROUP BY user_id ) AS t
GROUP BY device
13、昨天到目前為止每小時註冊人數
SELECT
DATE_FORMAT( create_time, '%Y/%m/%d %H' ) AS hours,
count( * ) AS count
FROM
user
WHERE
create_time < NOW( ) AND create_time > date_sub( NOW( ), INTERVAL 1 DAY )
GROUP BY
hours;