gpt4 book ai didi

php - 使用 SQL @variable 的 MySQL 查询结果字段为空

转载 作者:行者123 更新时间:2023-11-30 22:16:14 24 4
gpt4 key购买 nike

我有一个 MySQL 语句,它使用像“@StepWidth”这样的 MySQL 变量和在 IF 语句中使用的变量。

查询在 MySQL Workbench 中工作正常,但是当与 PHP 和 mysqli_query 一起使用时,带有变量的字段返回为空(空)。

想法是,根据 PHP 脚本设置的给定步长值将存储在数据库中的日期四舍五入到下一分钟、小时或天,并将结果按 unix_timestamp 分组用这个计算得到一个小的结果,以便稍后在图表中使用它。

查询

SELECT
@stepWidth:=900 as StepWidth,
task_template.puid,
task_template.ptemlate_name AS series_name,
round(avg(job.JOB_DURATION)) as value,
job.pcreation_date,
case
WHEN @stepWidth < 3600 THEN @RoundedCreationDate:=from_unixtime(UNIX_TIMESTAMP(job.pcreation_date) - MOD(UNIX_TIMESTAMP(job.pcreation_date), @stepWidth), "%Y-%m-%d %H:%i:%s")
WHEN @stepWidth >= 3600 AND @stepWidth < 86400 THEN @RoundedCreationDate:=DATE_ADD( DATE_FORMAT(job.pcreation_date, "%Y-%m-%d %H:00:00"), INTERVAL IF(MINUTE(job.pcreation_date) < 30, 0, 1) HOUR
)
WHEN @stepWidth >= 86400 then @RoundedCreationDate:=DATE_FORMAT(DATE_ADD( DATE_FORMAT(job.pcreation_date, "%Y-%m-%d %H:00:00"), INTERVAL IF(HOUR(job.pcreation_date) < 12, 0, 1) DAY ),"%Y-%m-%d 00:00:00" )
END AS RoundedCreationDate,
round(UNIX_TIMESTAMP(@RoundedCreationDate)) as start
FROM ( select @start := 0, @RoundedCreationDate := "1990-01-01" ) as Sqlvars, job
INNER JOIN task_template ON task_template.puid = job.rprocess_templateu
WHERE job.pcreation_date BETWEEN "2013-10-02 00:00:00" AND "2013-10-02 23:59:59"
GROUP BY task_template.puid,
task_template.ptemlate_name,"start"
ORDER BY task_template.ptemlate_name, "start";

我也曾尝试将结果写入临时表,但这带来了相同的结果。我希望有人能帮助我。非常感谢。

最佳答案

你似乎在混合一些东西。例如,您在 SELECT 语句中初始化 @stepWidth,而其他变量在子查询中初始化。

此外,您混淆了隐式和显式连接语法。这样做的结果是隐式连接的优先级较低,这可能会导致问题。

尝试切换到显式 CROSS JOIN 并在一个地方初始化变量:-

SELECT
@stepWidth AS StepWidth,
task_template.puid,
task_template.ptemlate_name AS series_name,
ROUND(AVG(job.JOB_DURATION)) AS value,
job.pcreation_date,
case
WHEN @stepWidth < 3600
THEN @RoundedCreationDate:=from_unixtime(UNIX_TIMESTAMP(job.pcreation_date) - MOD(UNIX_TIMESTAMP(job.pcreation_date), @stepWidth), "%Y-%m-%d %H:%i:%s")
WHEN @stepWidth >= 3600 AND @stepWidth < 86400
THEN @RoundedCreationDate:=DATE_ADD( DATE_FORMAT(job.pcreation_date, "%Y-%m-%d %H:00:00"), INTERVAL IF(MINUTE(job.pcreation_date) < 30, 0, 1) HOUR)
WHEN @stepWidth >= 86400 then @RoundedCreationDate:=DATE_FORMAT(DATE_ADD( DATE_FORMAT(job.pcreation_date, "%Y-%m-%d %H:00:00"), INTERVAL IF(HOUR(job.pcreation_date) < 12, 0, 1) DAY ),"%Y-%m-%d 00:00:00" )
END AS RoundedCreationDate,
ROUND(UNIX_TIMESTAMP(@RoundedCreationDate)) AS start
FROM job
INNER JOIN task_template ON task_template.puid = job.rprocess_templateu
CROSS JOIN
(
select @start := 0, @RoundedCreationDate := "1990-01-01", @stepWidth := 900
) AS Sqlvars
WHERE job.pcreation_date BETWEEN "2013-10-02 00:00:00" AND "2013-10-02 23:59:59"
GROUP BY task_template.puid,
task_template.ptemlate_name,
"start"
ORDER BY task_template.ptemlate_name,
"start";

请注意,由于您对行的分组方式,返回的字段确实存在更多潜在问题。例如,如果对于 puid/ptemlate_name 的值有多个 _job.pcreation_date_ 值,则不确定使用哪个。

关于php - 使用 SQL @variable 的 MySQL 查询结果字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38182043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com