公司的流程系统需要做查询,几张表分开的,想做成一张视图来用hibernate来做一个查询页面,根据条件查询出结果,但是在做视图的时候就出现难题,我用的sql语句创建视图的时候提示出错,查询了才知道是MySql视图不支持子查询,sql如下:
1 SELECT 2 `lsi`.`code` AS `CODE`, 3 `lsi`.`name` AS `zxmc`, 4 `lsi`.`zhuanxiantype` AS `zxlx`, 5 `lsi`.`user_bandwidth` AS `dk`, 6 `tu`.`XM` AS `wljl`, 7 `lsi`.`customer_manager_name` AS `khjl`, 8 `lsi`.`construction_id` AS `construction_id`, 9 `lsi`.`creator` AS `jdr`, 10 `lsi`.`created_date` AS `jdsj`, 11 `pd`.`cjsj` AS `fqjssj`, 12 `cp`.`bzmc` AS `dqhj`, 13 `cp`.`cjsj` AS `dqhjkssj`, 14 `cp`.`clr` AS `dqclr` 15 FROM 16 line_some_info lsi 17 LEFT JOIN 18 paidanriqi pd ON lsi.line_id = pd.line_id 19 LEFT JOIN 20 t_user tu ON lsi.net_manager = tu.BH 21 LEFT JOIN 22 (SELECT 23 * 24 FROM 25 (SELECT 26 lbi.line_id, wp.bzmc, rask.cjsj, tu.XM AS clr 27 FROM 28 workflow_rask_list rask 29 JOIN t_user tu ON rask.CLR = tu.BH 30 JOIN Prosess_Sample ps ON rask.bzslbh = ps.bzslbh 31 JOIN workflow_prosess wp ON ps.BZBH = wp.BZBH 32 JOIN Workflow_Prosess_Sample wps ON ps.lcslbh = wps.lcslbh 33 JOIN line_build_info lbi ON wps.item_Id = lbi.line_id 34 ORDER BY rask.cjsj DESC) ff 35 GROUP BY ff.line_id) cp ON lsi.line_id = cp.line_id
我试着将子查询(25行到34行)做成视图1,再把22行到35行cp位置做成视图2,再做1行到35行视图3.但是问题就出来了。
这个查询主要是想查询rask里面多条一样的line_id一致,但是创建时间 cjsj 最新的一条,单独执行这个sql是得到正确结果的。但是一旦做成视图1、2、3的嵌套,在视图1和视图2结合的时候,好像order by DESC 和group by line_id 就不能结合,得到的结果也不是cjsj的倒序。
我是初学者,不知道大家能不能看懂我的问题,求各位大神解答,我该怎么得到正确的结果的视图呢
…… A LEFT JOIN B 中B的顺序好像影响不到结果顺序
单独执行sql没问题,但是转成 视图来嵌套,22行select 嵌套25行select之后结果就不对了
mysql视图不支持子查询,真是日了狗了