1.sql join
a:從乙個和多個表中,根據指定的字段的關聯關係,查詢出資料
b:join 分為
inner join: 返回在兩個表中至少乙個匹配的行
left join:返回左表中的所有行,即使右表沒有匹配的記錄
right join:返回右表中的所欲行,即使左表沒有匹配的記錄
full join: 返回那些在其中乙個表中匹配的行
c:例子
2.行轉列的例子
2.1 成績表 將其 通過行轉列轉化如下結果
create table score(username varchar(100),
subject varchar(100),
score float);
初始化資料:
insert into score(username,subject,score)values('aa','chinese',100.0);
insert into score(username,subject,score)values('aa','math',90.0);
insert into score(username,subject,score)values('aa','eng',10.0);
insert into score(username,subject,score)values('ab','chinese',30);
insert into score(username,subject,score)values('ab','math',12);
要求輸出 [table]
|username chinese math, eng|
| aa 100.0 90.0 10.0|
| ab 30 12 0|
[/table]
這樣的結果。
mysql> select username,
(case subject when 'chinese' then score else 0 end) as 'chinese' , (case subject when 'math' then score else 0 end) as 'math',
(case subject when 'eng' then score else 0 end) as 'eng'
from score;
+----------+---------+------+------+
| username | chinese | math | eng |
+----------+---------+------+------+
| aa | 100 | 0 | 0 |
| aa | 0 | 90 | 0 |
| aa | 0 | 0 | 10 |
| ab | 30 | 0 | 0 |
| ab | 0 | 12 | 0 |
+----------+---------+------+------+
select a.username,
ifnull(sum(a.chinese),0) as 'chinese',
ifnull(sum(a.math),0) as 'math',
ifnull(sum(a.eng),0) as 'eng'
from(
select username,
(case subject when 'chinese' then score else 0 end) as 'chinese' ,
(case subject when 'math' then score else 0 end) as 'math',
(case subject when 'eng' then score else 0 end) as 'eng'
from score
) agroup by a.username
或者修改為:
select username,
max(case subject when 'chinese' then score else 0 end) as 'chinese' , max(case subject when 'math' then score else 0 end) as 'math',
max(case subject when 'eng' then score else 0 end) as 'eng'
from score group by username;
3.列轉行的例子 通過union all
select progrectname,'oversea'as supply,overseasupply as total from progrectdetail
union all
select progrectname,'native' as supply ,nativesupply as total from progrectdetail
union all
select progrectname,'south' as supply ,southsupply as total from progrectdetail
union all
select progrectname,'north'as supply,northsupply as total from progrectdetail;
4.刪除重複行的例子
a:先創造乙個空的備份表
create table progrectdetail_bak where 1=0;
b:將源表中的資料group by 出來 insert 新錶中
insert into progrectdetail_bak
select a.progrectname ,a.overseasupply,a.nativesupply,a.southsupply,a.northsupply
from(
select progrectname ,overseasupply,nativesupply,southsupply,northsupply,count(*) as cn
from progrectdetail
group by progrectname ,overseasupply,nativesupply,southsupply,northsupply
)a
c: 刪除源表
drop table progrectdetail
d:將備份表重新命名為源表
alter table progrectdetail_bak rename to progrectdetail;
a: 首先為原來的表新增偽列
5.構造偽列
select @rownum:=@rownum+1 as rownum, progrectdetail.*
from (select @rownum:=0) r, progrectdetail
HiveQL之Database相關操作
1 create database 建立資料庫語法 create database schema if not exists database name comment database comment location hdfs path with dbproperties property na...
Database 物理檔案
1 乙個資料庫至少需要乙個控制檔案 2 控制檔案 二進位制檔案 是乙個很小的 通常是資料庫中最小的 檔案,大小一般在1 5m左右。3 在資料庫的執行過程中,每當出現資料庫檢查點或修改資料庫的結構後,oracle就會修改控制檔案的內容。4 dba可以通過oem工具修改控制檔案中的部分內容,但dba和使...
beego關聯操作
一對多 反向多對一 結構體 sysgroup 群組 type sysgroup struct sysgroupmember 群組成員 type sysgroupmember struct 關聯插入思路formdata sysgroupmember 關係n fkgroup sysgroup 關係1 f...