吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1355|回复: 21
收起左侧

[求助] SQL复杂查询,请大佬帮忙检查一下SQL语法是不是有错误,用的语言是C#

[复制链接]
alwaysalone 发表于 2020-4-25 20:51
[SQL] 纯文本查看 复制代码
//是否有大小写
            //OP1查询
            String op1_qtime = "select * from op1 where time between '"+start+ "' and '" + end + "'";//查询最后一个记录
            String op1_result = "select temp.code,temp.zz,temp.xm,temp.time,max(temp.id) from ('"+op1_qtime+"') as temp group by temp.code";//嵌套查询
            //OP2查询
            String op2_qtime = "select * from op2 where time between '" + start + "' and '" + end + "'";//查询最后一个记录
            String op2_result = "select temp.code,temp.hd,temp.rj,temp.jd,temp.ds,temp.time,max(temp.id) from ('" + op2_qtime + "') as temp group by temp.code";//嵌套查询
            //OP3查询
            String op3_qtime = "select * from op3 where time between '" + start + "' and '" + end + "'";//查询最后一个记录
            String op3_result = "select temp.code,temp.flowstatus,temp.speedstatus,temp.flow,temp.speed,temp.voice,temp.power,temp.time,max(temp.id) from ('" + op3_qtime + "') as temp group by temp.code";//嵌套查询
            //OP4查询
            String op4_qtime = "select * from op4 where time between '" + start + "' and '" + end + "'";//查询最后一个记录
            String op4_result = "select temp.code,temp.resis,temp.interval,temp.time,max(temp.id) from ('" + op4_qtime + "') as temp group by temp.code";//嵌套查询
            //OP5查询
            String op5_qtime = "select * from op5 where time between '" + start + "' and '" + end + "'";//查询最后一个记录
            String op5_result = "select temp.code,temp.interval,temp.switch,temp.time,max(temp.id) from ('" + op5_qtime + "') as temp group by temp.code";//嵌套查询
            //OP6查询
            String op6_qtime = "select * from op6 where time between '" + start + "' and '" + end + "'";//查询最后一个记录
            String op6_result = "select temp.code,temp.flowstatus,temp.speedstatus,temp.flow,temp.speed,temp.direction,temp.voice,temp.surface,temp.defect,temp.outline,temp.damage,temp.code1,temp.shell,temp.time,max(temp.id) from ('" + op6_qtime + "') as temp group by temp.code";//嵌套查询
            //full join
            //idea 不如Datagrid加入6个时间,,,嵌套查询要括号括起来
            String op1_op2 = "select op1.code as a,op1.zz as b,op1,zm as c,op1.time as op1time,op2.hd as d,op2.rj as e,op2.jd as f,op2.ds as g,op2.time as op2time from ('"+op1_result+"') as op1 inner join ('"+op2_result+"') as op2 on op1.code=op2.code";
            String op1_op2_op3 = "select op12.a,op12.b,op12.c,op12.op1time,op12.d,op12.e,op12.f,op12.g,op12.op2time,op3.flowstatus as h,op3.speedstatus as i,op3.flow as j,op3.speed as k,op3.voice as l,op3.power as m,op3.time as op3time from ('"+op1_op2+"') as op12 inner join ('"+op3_result+"') as op3 on op12.a=op3.code";
            String op1_op2_op3_op4 = "select op123.a,op123.b,op123.c,op123.op1time,op123.d,op123.e,op123.f,op123.g,op123.op2time,op123.h,op123.i,op123.j,op123.k,op123.l,op123.m,op123.op3time,op4.resis as n,op4.interval as o,op4.time as op4time from('"+op1_op2_op3+"') as op123 inner join ('"+op4_result+"') as op4 on op123.a=op4.code";
            String op1_op2_op3_op4_op5 = "select op1234.a,op1234.b,op1234.c,op1234.op1time,op1234.d,op1234.e,op1234.f,op1234.g,op1234.op2time,op1234.h,op1234.i,op1234.j,op1234.k,op1234.l,op1234.m,op1234.op3time,op1234.n,op1234.o,op1234.op4time,op5.interval as p,op5.switch as q,op5.time as op5time from ('"+op1_op2_op3_op4+"') as op1234 inner join ('"+op5_result+"') as op5 on op1234.a=op5.code";
            String op1_op2_op3_op4_op5_op6 = "select op12345.a,op12345.b,op12345.c,op12345.op1time,op12345.d,op12345.e,op12345.f,op12345.g,op12345.op2time,op12345.h,op12345.i,op12345.j,op12345.k,op12345.l,op12345.m,op12345.op3time,op12345.n,op12345.o,op12345.op4time,op12345.p,op12345.q,op12345.op5time,op6.flowstatus as r,op6.speedstatus as s,op6.flow as t,op6.speed as u,op6.direction as v,op6.voice as w,op6.surface as x,op6.defect as y,op6.outline as z,op6.damage as aa,op6.code1 as bb,op6.shell as cc,op6.time as op6time from ('"+op1_op2_op3_op4_op5+"') as op12345 inner join ('"+op6_result+"') as op6 on op12345.a=op6.code";
            //result结构a,b,c,op1time,d,e,f,g,op2time,h,i,j,k,l,m,op3time,n,o,op4time,p,q,op5time,r,s,t,u,v,w,x,y,z,aa,bb,cc,op6time
            SqlConnection sqlCnt = new SqlConnection(conn);
            sqlCnt.Open();
            SqlCommand sql = new SqlCommand(op1_op2_op3_op4_op5_op6, sqlCnt);

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

qoovoop 发表于 2020-4-25 21:05
有group by的语句除了聚合函数和自己,select后面不能有别的字段!
 楼主| alwaysalone 发表于 2020-4-25 21:23
qoovoop 发表于 2020-4-25 21:05
有group by的语句除了聚合函数和自己,select后面不能有别的字段!

啥意思。不懂,能否说详细点哦
 楼主| alwaysalone 发表于 2020-4-25 21:30
qoovoop 发表于 2020-4-25 21:05
有group by的语句除了聚合函数和自己,select后面不能有别的字段!

我感觉标点符号还有些问题,大佬能指点一下?
Yoona520 发表于 2020-4-25 21:34
C#没有ORM框架可以用吗,这么写也太抽象了
 楼主| alwaysalone 发表于 2020-4-25 21:42
Yoona520 发表于 2020-4-25 21:34
C#没有ORM框架可以用吗,这么写也太抽象了

用微软自带裤
hestyle 发表于 2020-4-25 21:42
第4、7、10...拼接SQL的时候是不是多了单引号,from ('"+op1_qtime+"') as,op1_qtime是子查询,用括号括起来就行

一楼老兄的话的意思是,使用了group by进行分组后,select只能查询分组的字段、聚合函数
比如第4行,你根据op1_qtime子查询的结果temp表,按照code分组后,你select后面只能查temp.code,以及max(temp.id)等其它使用聚合函数的结果,不能查temp.time这种
hestyle 发表于 2020-4-25 21:45
写这么长的sql,还不好好格式化一下,可读性太差了,碰到关键字就换个行啊,比如

SELECT name1,name2,name3
FROM table1
WHERE A and B
 楼主| alwaysalone 发表于 2020-4-25 21:50
hestyle 发表于 2020-4-25 21:45
写这么长的sql,还不好好格式化一下,可读性太差了,碰到关键字就换个行啊,比如

SELECT name1,name2,na ...

我原意是,因为一张表里有重复数据,但是我只取指定时间内的最后的那一条的所有字段,所以用group by,一楼说的错误我明白了,可是,因为要取记录的所有字段又不知道如何是好了
hestyle 发表于 2020-4-25 21:52
alwaysalone 发表于 2020-4-25 21:50
我原意是,因为一张表里有重复数据,但是我只取指定时间内的最后的那一条的所有字段,所以用group by,一 ...

用where + between筛选,再按时间降序排列,然后用limit关键字,取出第一行(区间中时间最大的)即可

不过你用的是什么数据库管理系统,mysql还是啥?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 18:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表