mysql数据库视图列子参考_MySQL视图小例子

本文介绍了如何使用MySQL视图来解决查询接口字段不匹配的问题。通过创建视图,将表中的字段映射为与查询接口一致的名称,从而保持查询接口的稳定性。示例包括带字段判断和字符串连接的视图、字符串截取的视图、联表查询的视图以及创建自定义字符串分割函数的视图应用。
摘要由CSDN通过智能技术生成

场景:

某查询接口 查询sql语句已确定,用该sql语句去查 表 t_strategy_stock 中的数据,但是 表t_strategy_stock 的字段名称和 sql 语句中写死的名称不同。

需求:

查询接口不做改动,sql语句不变,从表 t_strategy_stock 中查出数据。

解决方法:

新建视图,把 表t_strategy_stock 中的字段名称映射成 和查询接口 SQL 语句中相同的名称,然后从视图中查询数据。

93841ccf95442a53165b7eb191d3a5a9.png

0843d436293c9682b8052c0dc8ed7b98.png

1.带字段判断和字符串连接 concat 的例子:

select `t`.`formula_id` AS `gscode`,concat(convert(if((`t`.`market` = 0),'SZ','SH') using utf8),`t`.`code`) AS `gpcode`,`t`.`date` AS `ymd`,`t`.`time` AS `hms`,`t`.`price` AS `price`,`t`.`zdf` AS `zdf`

from `t_strategy_stock` `t`

表 t_strategy_stock :

45c8c001072b4265a4c926e630606c3b.png

视图 v_strategy_stock :

c6ebb63810ba29c21179df276985079e.png

2.带字符串截取substr的例子:

select `t`.`formula_id` AS `formula_id`, `t`.`period_type` AS `period_type`, `t`.`ymd` AS `ymd`, if( (convert(substring(`t`.`gpcode`,1,2)using utf8) = 'SZ'), 0,1) AS `gpMarket`, convert(substring(`t`.`gpcode`,3,6)using utf8) AS `gpcode`, `t`.`dayNum` AS `dayNum`

from `signaldata` `t`

表 signaldata:

4a0797beb8b7580992816185d6c07a56.png

视图 v_signaldata:

ff6c68a266970596c661e32529a33495.png

select case convert(`t`.`gscode` using utf8)

when 'DYYH' then 37

when 'DYEH' then 38

when 'HJY' then 39

when 'PKS' then 40 end

AS `formula_id`, 7 AS `period_type`, `t`.`ymd` AS `ymd`, if( (convert(substring(`t`.`gpcode`,1,2)using utf8) = 'SZ'), 0,1) AS `gpMarket`, convert(substring(`t`.`gpcode`,3,6)using utf8) AS `gpcode`, `t`.`f1` AS `f1`, `t`.`f2` AS `f2`, `t`.`f3` AS `f3`, `t`.`f4` AS `f4`, `t`.`f5` AS `f5`, `t`.`f6` AS `f6`, `t`.`f7` AS `f7`, `t`.`f8` AS `f8`, `t`.`f9` AS `f9`, `t`.`f10` AS `f10`, `t`.`f11` AS `f11`, `t`.`f12` AS `f12`, `t`.`f13` AS `f13`, `t`.`f14` AS `f14`, `t`.`f15` AS `f15`, `t`.`f16` AS `f16`, `t`.`rise_max` AS `rise_max`, `t`.`rise_signalDay` AS `rise_signalDay`, `t`.`high_max` AS `high_max`, `t`.`yClose_signalDay` AS `yClose_signalDay`

from `calcgsdataflash` `t`

表:calcgsdataflash

5213f994657e7ad6cac938936bfd9582.png

视图:v_calcgsdataflash

e86c6fec44388f94b908197924b42a6a.png

select case formula_id

when '4' then 'DXJJ' end

AS `gscode`,concat(convert(if((`t`.`gpMarket` = 0),'SZ','SH') using utf8),`t`.`gpcode`) AS `gpcode`,`t`.`ymd` AS `ymd`,

( floor(`t`.`hms` / 10000) * 60 + floor(`t`.`hms` % 10000 / 100) ) AS `hms`,

`t`.`f1` AS `f1`, `t`.`f2` AS `f2`, `t`.`f3` AS `f3`, `t`.`f4` AS `f4`, `t`.`f5` AS `f5`, `t`.`f6` AS `f6`, ( floor(`t`.`f7` / 10000) * 60 + floor(`t`.`f7` % 10000 / 100) ) AS `f7`, `t`.`f8` AS `f8`, `t`.`f9` AS `f9`, `t`.`f10` AS `f10`, `t`.`f11` AS `f11`, `t`.`f12` AS `f12`, `t`.`f13` AS `f13`, `t`.`f14` AS `f14`, `t`.`f15` AS `f15`, `t`.`f16` AS `f16`, `t`.`rise_max` AS `rise_max`, `t`.`rise_signalDay` AS `rise_signalDay`, `t`.`high_max` AS `high_max`, `t`.`yClose_signalDay` AS `yClose_signalDay`

from `stockpooldata_flash` `t`

3.联表查询新建视图

场景:需要从表 t_fid_254获取 market、code、date、buy、sell,从表 stockqt 获取 name字段

select `t`.`market` AS `market`,`t`.`code` AS `code`,`t`.`date` AS `date`,substr(`t`.`data`,2,1) AS `buy`,substr(`t`.`data`,(length(`t`.`data`) - 1),1) AS `sell`,

`qt`.`gpName` AS `name`

from (`t_fid_254` `t` left join `stockqt` `qt` on(((`t`.`code` = `qt`.`gpcode`) and (`t`.`market` = `qt`.`gpMarket`))))

表 t_fid_254:

05d7df62a055bb98fabac847eb541d9a.png

表 stockqt:

222f2f2bc685642aea09bea53de87524.png

新建的视图 v_t_fid_254:

39d4d9d2d679e56839aa214838423838.png

4.创建按指定字符分割字符串函数

场景:需要将data字符串( 格式:[1,-1895154,,1] )按逗号分割

解决方法:

1.创建一个自定义函数 SPLIT_STR 完成分割功能

d898e2254223250ba087e246b30b192e.png

CREATE FUNCTION SPLIT_STR(

x VARCHAR(255),

delim VARCHAR(12),

pos INT

)

RETURNS VARCHAR(255)

RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),

LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),

delim, '');

2.使用SPLIT_STR函数按逗号分割data

SELECT `t`.`market` as `market`,

`t`.`code` as `code`,

`t`.`date` as `date`,

`t`.`time` as `time`,

SPLIT_STR(`t`.`data`, ',', 2) as `ZJQD`,

SPLIT_STR(`t`.`data`, ',', 3) as `ZLZB`,

SPLIT_STR(`t`.`data`, ',', 4) as `SHZB`,

SPLIT_STR(SPLIT_STR(`t`.`data`, ',', 6), ']', 1) as `ZJQDZB`,

`qt`.`gpName` as `name`

from (`t_fid_8` as `t` left join `stockqt` as `qt` on((`t`.`code` = `qt`.`gpcode`) and (`t`.`market` = `qt`.`gpMarket`)) )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值