這篇文章主要講解了“MySQL交叉表的寫法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“mysql交叉表的寫法”吧!

創新互聯公司專業為企業提供雨城網站建設、雨城做網站、雨城網站設計、雨城網站制作等企業網站建設、網頁設計與制作、雨城企業網站模板建站服務,十多年雨城做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
創建2張表 一張t_shuiguo 水果表 一張t_supermarket 超市表
現在我要查一個超市的各區水果價格的匯總
如下: 表A
那么首先水果表 是可以動態添加的 所有A表中的列 是動態的 先不考慮
先看下靜態的 如果就是這么4個水果
那么SQL可以這么寫 (參考了網上一些列子)
-- 靜態sql
01
select ifnull(groups,'total') as groups,
02
03
sum(if(name='蘋果',prices,0)) as '蘋果',
04 www.2cto.com
05
sum(if(name='梨',prices,0)) as '梨',
06
07
sum(if(name='橘子',prices,0)) as '橘子',
08
09
sum(if(name='櫻桃',prices,0)) as '櫻桃',
10
11
sum(if(name='total',prices,0)) as 'totals'
12
13
from
14
15
(select A.groups as groups,IFNULL(A.name,'total') as name ,sum(A.price) as prices
16
17
from
18
19
(select
20
21
m.groups as groups ,s.name as name,m.price as price
22
23
from t_supermarket m
24
25
inner join t_shuiguo s
26
27
on m.shuiguo = s.id
28 www.2cto.com
29
) A
30
31
group by groups, name
32
33
with rollup
34
35
having groups is not null
36
37
) B
38
39
group by groups
40
41
with rollup
然后比較費勁的就是動態的 需要用到存儲過程
如下:
001
-- 定義存儲過程結束符
002
003
delimiter $$
004
005
-- 有先刪除 再創建過程
006
007
drop procedure if exists searchShuiguo $$
008
009
create procedure searchShuiguo()
010
011
begin
012
013
-- 定義sql前端
014
015
declare v_1 varchar(1000) default ' SELECTIFNULL(groups,\'total\') as groups ';
016
017
-- 定義sql 后端
018
019
declare v_2 varchar(1000) default ' from (select groups,IFNULL(code,\'total\') as code ,sum(A.price) as prices www.2cto.com
020
021
from (
022
023
selectm.groups as groups ,s.code as code,m.price as price
024
025
from t_supermarket m inner join t_shuiguo s on m.shuiguo = s.id
026
027
) A
028
029
group by groups,
030
031
code with rollup having groups is not null
032
033
) B
034
035
group by groups
036
037
with rollup';
038
039
-- 定義臨時參數
040
041
declare v_temp varchar(2000);
042
043
-- 定義要遍歷的變量
044 www.2cto.com
045
declare v_shuiguo varchar(100) ;
046
047
-- 定義結束變量
048
049
declare stop int default 0;
050
051
-- 定義游標 去查水果列表
052
053
declare cur cursor for select code from t_shuiguo ;
054
055
-- 一個沒找到的回調設置
056
057
declare continue handler for not found set stop = 1;
058
059
-- 游標 遍歷 拼接sql字符串
060
061
OPEN cur;
062
063
FETCH cur INTO v_shuiguo;
064 www.2cto.com
065
WHILE stop = 0
066
067
DO
068
069
if v_temp = '' then
070
071
set v_temp = CONCAT(v_1,'sum(if(code =\'',v_shuiguo,'\'');
072
073
set v_1 = CONCAT(v_temp,',prices,0)) as ',v_shuiguo);
074
075
else
076
077
set v_temp = '';
078
079
set v_temp = CONCAT(v_1,',','sum(if(code =\'',v_shuiguo,'\'','');
080
081
set v_1 = CONCAT(v_temp,',prices,0)) as ',v_shuiguo);
082 www.2cto.com
083
end if;
084
085
FETCH cur INTO v_shuiguo;
086
087
END WHILE;
088
089
CLOSE cur;
090
091
set @v_result = CONCAT(v_1,', sum(if(code=\'total\',prices,0)) as \'total\'');
092
093
set @v_result = CONCAT(@v_result,v_2);
094
095
-- 執行sql
096
097
prepare stmt from @v_result;
098
099
EXECUTE stmt ;
100
www.2cto.com
101
deallocate prepare stmt;
102
103
end $$
哦了
call searchShuiguo () 就可以了
感謝各位的閱讀,以上就是“mysql交叉表的寫法”的內容了,經過本文的學習后,相信大家對mysql交叉表的寫法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創新互聯,小編將為大家推送更多相關知識點的文章,歡迎關注!
網頁名稱:mysql交叉表的寫法
瀏覽路徑:http://www.yijiale78.com/article38/gdccpp.html
成都網站建設公司_創新互聯,為您提供電子商務、微信公眾號、標簽優化、虛擬主機、用戶體驗、網站改版
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯