先介绍一个简单的关键字(concatenate).将不同的字段的所对应的值可以拼接在一起。简单的先用一个简单的示例来演示一下。
首先我们可以看看一个实验的表格:
上面的表格中有一些数据。例如我们想将id和描述信息放在同一个表格中,此时我们就可以使用concatenate这个关键字来将两个字段进行联合起来:
从上图可以看出,我们在使用concat的使用,可以将任意的字段拼接在一起。中间可以加上我们想要填充的内容。在上面的语句中,我添加了一个“:”来分割id和desc信息。
有时候我们对于新生成的一个字段可以给他设置一个别名,就像上面的 concat (prod_id,':',prod_desc) ,这样看起来并不是很友好。我们现在可以使用as关键字来进行对检索出来的内容进行重命名。直接看结果:
从上图我们可以看出,将 concat (prod_id,':',prod_desc) 的结果改为一个新的名字, prod_total_desc .
当然可以,就是在mysql存储过程中使用动态sql,就可以拼接sql,然后执行了。
给你复制一段,如果不满意,自己搜索 mysql 存储过程 动态sql 就可以了
DROP PROCEDURE IF EXISTS SearchByDoctorCREATE PROCEDURE SearchByDoctor(
IN DoctorId VARCHAR(50),
IN deptId VARCHAR(50),
IN beginDate VARCHAR(20),
IN endDate VARCHAR(20),
IN StandDeptId VARCHAR(50),
IN OperationFlag VARCHAR(50),
IN SsczflId VARCHAR(50),
OUT OperNum INT,
OUT AvgDangerIndex DOUBLE,
OUT OperGrCase INT
)
BEGIN
DECLARE cal1 VARCHAR(800)
DECLARE cal2 VARCHAR(800)
SET cal1="SELECT COUNT(1),AVG(DANGER_INDEX) INTO @para1,@para2 FROM yw_ssxxb WHERE 1=1 "
SET cal2="SELECT COUNT(1) INTO @para3 FROM gr_grbw WHERE OPE_RELID IN (SELECT RELID FROM yw_ssxxb WHERE 1=1"
#拼接医生id
SET cal1=CONCAT(cal1," ","AND OPEDOC_ID = ","'",DoctorId,"'")
SET cal2=CONCAT(cal2," ","AND OPEDOC_ID = ","'",DoctorId,"'")
#拼接科室id
IF deptId <>'' THEN
SET cal1=CONCAT(cal1,"AND DEPT_ID =","'",deptId,"'")
END IF
#拼接开始结束日期
IF beginDate<>'' AND endDate <>'' THEN
SET cal1=CONCAT(cal1," ","AND OPER_AT BETWEEN ","'",beginDate,"'"," AND ","'",endDate,"'")
END IF
#拼接标准科室
IF StandDeptId<>'' THEN
SET cal1=CONCAT(cal1," ","AND DEPT_ID IN ( SELECT ID FROM department WHERE STAND_DEPT_ID = ","'",StandDeptId,"'",")")
END IF
#拼接数据来源
IF OperationFlag <>'' THEN
SET cal1=CONCAT(cal1," ","AND OPEPARTKINDID IN (SELECT ID FROM zh_ssczfl WHERE FLAG= ","'",OperationFlag,"'",")")
END IF
#拼接手术 *** 作类别
IF SsczflId <> '' THEN
SET cal1=CONCAT(cal1," ","AND OPEPARTKINDID = ","'",SsczflId,"'")
END IF
SET cal2 =CONCAT(cal2,")")
SET @sql1=cal1
SET @sql2=cal2
PREPARE stmt1 FROM @sql1
EXECUTE stmt1
DEALLOCATE PREPARE stmt1
PREPARE stmt2 FROM @sql2
EXECUTE stmt2
DEALLOCATE PREPARE stmt2
SET OperNum=@para1
SET AvgDangerIndex=@para2
SET OperGrCase=@para3
END
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)