虚拟表的使用和创建

格式

with recursive 表名字 as(查询语句)
表明b as(查询语句) 
select * from 表名字,表明b

示例

with recursive a as ( SELECT count(1) as dw FROM xnb_hn_zyjc_main_process WHERE cczg_sfdw=1 and sheng='湖南省' ), 
b as ( SELECT count(1) as zgbdw FROM xnb_hn_zyjc_main_process WHERE cczg_sfdw=2 and sheng='湖南省' )
SELECT * FROM a,b

查虚拟表

with statis as 
( select total_length, total_time, area_code from patrol_analysis_statistics )


select t_.area_name as xzqName, t_.area_name as name, 
(select round(sum(total_length) / 1000, 2) from statis) as mileage, 


(select sum(total_time) from statis) as duration

 from sys_area t_ 

不支持with 关键字

SELECT  
    a.fiwtons + b.total_fiwtons AS total_fiwtons,
    a.fontons + b.fontons AS total_fontons,
    a.fintons + b.fintons AS total_fintons,
    a.fowtons + b.total_fowtons AS total_fowtons,
		a.fcname as fcname
FROM (
    SELECT
        tb_throughput_total.fiwtons, 
        tb_throughput_total.fontons, 
        tb_throughput_total.fintons, 
        tb_throughput_total.fowtons,
				tb_throughput_total.fbegintime,
					tb_throughput_total.fendtime,
        tb_cargo_type.fname AS fcname
    FROM
        tb_throughput_total
    LEFT JOIN
        tb_cargo_type
    ON 
        tb_throughput_total.fcargoid = tb_cargo_type.id
    WHERE
        fdept = '24' 
        AND MONTH(tb_throughput_total.fbegintime) = 1 
        AND MONTH(tb_throughput_total.fendtime) = MONTH(CURRENT_DATE()) 
        AND YEAR(tb_throughput_total.fbegintime) = YEAR(CURRENT_DATE())  -1
        AND YEAR(tb_throughput_total.fendtime) = YEAR(CURRENT_DATE()) -1
) AS a
JOIN (
    SELECT
        SUM(tb_throughput.fiwtons) AS total_fiwtons, 
        SUM(tb_throughput.fontons) AS fontons, 
        SUM(tb_throughput.fintons) AS fintons, 
        SUM(tb_throughput.fowtons) AS total_fowtons,
        tb_cargo_type.fname AS fcname,
				tb_throughput.fmonth
    FROM
        tb_throughput
    LEFT JOIN
        tb_cargo_type
    ON 
        tb_throughput.fcargoid = tb_cargo_type.id
    WHERE
        SUBSTRING(tb_throughput.fmonth, 1, 4) = YEAR(CURRENT_DATE()) - 1
    AND SUBSTRING(tb_throughput.fmonth, 6, 2) = MONTH(CURRENT_DATE())
        AND tb_throughput.fisvalid = 1   
        AND (fdept = 17)
    GROUP BY 	
        tb_cargo_type.fname, tb_throughput.fmonth
) AS b 
ON a.fcname = b.fcname;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值