MySql Select 变量的声明和使用
MySql Declaration and Use of Select Variables
我很难以正确的方式编写查询。虽然 它确实有效 我收到错误消息:
A new statement was found but no delimiter between it and the previous one (near select)
显然我仍然可以处理这个错误,但是 #1。我不是特别喜欢 'hacky' 应对方法和#2。它在 php 进一步尝试 运行 查询的行中引起问题。
来自 Google 的研究表明错误 可能是一个错误 但是我怀疑这一点。
我实际上想做的事情:
我有 3 个表:
Table P [Process_ID+, Process_Name, Risk_ID*]
Table V [Validation_ID, Process_ID+, Validation_Date]
Table R [Risk_ID*, Risk_TimePeriod]
计划是:
- Select 进程详情
- 加入验证详细信息
- 加入风险详情
- 根据 Validation_Date 和 Risk_TimePeriod 生成 "Due_Date"(即,如果时间段为 150,则向 Validation_Date 添加 150 天)。
类似于下面的内容:
Process_ID | Process_Name | Validation_Date | Due_Date | Due_Days | Risk_Level
1 My_Process 2017-02-17 2017-07-17 -150 High
所以在这里; Due_Date 是通过将 150 (Risk_Level) 添加到验证日期创建的。
当前查询
Set @TimePeriod =
(Select r.Risk_TimePeriod
from processes_active p
inner join processes_risk_config r
on r.Risk_ID = p.Process_Risk
where p.Process_ID = 2);
Select p.Process_ID,
p.Process_Name,
v.Validation_Date,
Date_Add(v.Validation_Date, interval @TimePeriod Day) as Due_Date,
Datediff(Now(), Date_Add(v.Validation_Date, interval @TimePeriod Day)) as Due_Days,
r.Risk_Level
From processes_active p
left JOIN processes_validations v
on p.Process_ID = v.Validation_Process_ID
inner join processes_risk_config r
on r.Risk_ID = p.Process_Risk
Where p.Process_ID = 2
Order By v.Validation_Date Desc
我的第一个 "Select" 子句以红色突出显示。这是出现错误消息的地方。如果有人能指出我哪里出错了,将不胜感激!谢谢
原来我over-complicating完全就是这样!
新工作查询:
Select
p.Process_ID,
p.Process_Name,
v.Validation_Date,
r.Risk_TimePeriod,
Date_Add(v.Validation_Date, interval r.Risk_TimePeriod Day) as Due_Date,
Datediff(Now(), Date_Add(v.Validation_Date, interval r.Risk_TimePeriod Day)) as Due_Days,
r.Risk_Level
From pdc_processes.processes_active p
left JOIN pdc_processes.processes_validations v
on p.Process_ID = v.Validation_Process_ID
inner join processes_risk_config r
on r.Risk_ID = p.Process_Risk
Where p.Process_ID = 2
Order By v.Validation_Date Desc
我很难以正确的方式编写查询。虽然 它确实有效 我收到错误消息:
A new statement was found but no delimiter between it and the previous one (near select)
显然我仍然可以处理这个错误,但是 #1。我不是特别喜欢 'hacky' 应对方法和#2。它在 php 进一步尝试 运行 查询的行中引起问题。
来自 Google 的研究表明错误 可能是一个错误 但是我怀疑这一点。
我实际上想做的事情:
我有 3 个表:
Table P [Process_ID+, Process_Name, Risk_ID*]
Table V [Validation_ID, Process_ID+, Validation_Date]
Table R [Risk_ID*, Risk_TimePeriod]
计划是:
- Select 进程详情
- 加入验证详细信息
- 加入风险详情
- 根据 Validation_Date 和 Risk_TimePeriod 生成 "Due_Date"(即,如果时间段为 150,则向 Validation_Date 添加 150 天)。
类似于下面的内容:
Process_ID | Process_Name | Validation_Date | Due_Date | Due_Days | Risk_Level
1 My_Process 2017-02-17 2017-07-17 -150 High
所以在这里; Due_Date 是通过将 150 (Risk_Level) 添加到验证日期创建的。
当前查询
Set @TimePeriod =
(Select r.Risk_TimePeriod
from processes_active p
inner join processes_risk_config r
on r.Risk_ID = p.Process_Risk
where p.Process_ID = 2);
Select p.Process_ID,
p.Process_Name,
v.Validation_Date,
Date_Add(v.Validation_Date, interval @TimePeriod Day) as Due_Date,
Datediff(Now(), Date_Add(v.Validation_Date, interval @TimePeriod Day)) as Due_Days,
r.Risk_Level
From processes_active p
left JOIN processes_validations v
on p.Process_ID = v.Validation_Process_ID
inner join processes_risk_config r
on r.Risk_ID = p.Process_Risk
Where p.Process_ID = 2
Order By v.Validation_Date Desc
我的第一个 "Select" 子句以红色突出显示。这是出现错误消息的地方。如果有人能指出我哪里出错了,将不胜感激!谢谢
原来我over-complicating完全就是这样!
新工作查询:
Select
p.Process_ID,
p.Process_Name,
v.Validation_Date,
r.Risk_TimePeriod,
Date_Add(v.Validation_Date, interval r.Risk_TimePeriod Day) as Due_Date,
Datediff(Now(), Date_Add(v.Validation_Date, interval r.Risk_TimePeriod Day)) as Due_Days,
r.Risk_Level
From pdc_processes.processes_active p
left JOIN pdc_processes.processes_validations v
on p.Process_ID = v.Validation_Process_ID
inner join processes_risk_config r
on r.Risk_ID = p.Process_Risk
Where p.Process_ID = 2
Order By v.Validation_Date Desc