[英]MySQL - Variable in WHERE clause result from SELECT statement
I have this query: 我有这个查询:
INSERT INTO tab_reminder (ID_cliente, ID_articolo, Data_acquisto)
SELECT c.ID_cliente as cliente, ca.ID_articolo as articolo, MAX(c.Data_contratto) as ultimoAcquisto
FROM tab_contratti_articoli ca
LEFT JOIN tab_contratti c ON c.ID_contratto = ca.ID_contratto
LEFT JOIN tab_articoli a ON ca.ID_articolo = a.ID_articolo
LEFT JOIN tab_clienti cl ON c.ID_cliente = cl.ID_cliente
WHERE c.ID_cliente != 0
AND cl.ID_utente = 0
AND Giorni_reminder != 0
AND c.Data_contratto <= DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)
AND c.Data_contratto > DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY), INTERVAL 60 DAY)
AND (c.Data_contratto NOT BETWEEN DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY) AND '2017-05-16')
GROUP BY c.ID_cliente, ca.ID_articolo
ON DUPLICATE KEY UPDATE ID_cliente=ID_cliente, ID_articolo=ID_articolo, Data_acquisto=Data_acquisto, Articolo_visibile=Articolo_visibile;
Basically, it annoys me that I have to repeat DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)
date result. 基本上,我不得不重复DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)
日期结果,这使我DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)
。
Is there a way to declare a variable in MySQL for that date? 有没有办法在该日期在MySQL中声明变量?
Something like: 就像是:
SET @newDate := DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY);
And then use it like this: 然后像这样使用它:
AND c.Data_contratto <= @newDate
AND c.Data_contratto > DATE_SUB(@newDate, INTERVAL 60 DAY)
AND (c.Data_contratto NOT BETWEEN @newDate AND '2017-05-16')
I have searched and for what I have found, there is no way or it's not allowed to declare variables or aliases in the WHERE
clause, but maybe there is a workaround to avoid typing the same thing several times in the same query. 我已经搜索过,对于所发现的内容,没有办法,或者不允许在WHERE
子句中声明变量或别名,但是也许有一种解决方法,可以避免在同一查询中多次键入相同的内容。
As always, thank you all and have a nice day! 与往常一样,谢谢大家,祝您愉快!
Update 1: 更新1:
I have seen @TimBiegeleisen's link and I find it very, very interesting. 我看过@TimBiegeleisen的链接,我发现它非常非常有趣。 But the project I work in uses deprecated mysql API to query. 但是我从事的项目使用不推荐使用的mysql API进行查询。 See example: 参见示例:
$sqlExample = "SELECT * FROM tab_example";
$qExample = mysql_query($sqlExample) or die ("Error in $sqlExample: ".mysql_error());
$rsExample = mysql_fetch_object($qExample);
Taking this into consideration, are still Prepared statements viable? 考虑到这一点, Prepared语句仍然可行吗?
Returning to OP, Is it a way of declaring the variable in the query where I need it (In WHERE
clause) ? 返回OP,这是在查询中我需要的地方(在WHERE
子句中)声明变量的方法吗?
Update 2: 更新2:
This is a clarification of why this: How to declare a variable in MySQL? 这是为什么的澄清: 如何在MySQL中声明变量?
Is not working for me or I am not managing to make it work. 对我不起作用,或者我没有设法使其起作用。
I don't have to declare static data, I would like to declare a variable that is result of a SELECT. 我不必声明静态数据,我想声明一个SELECT结果的变量。
The data I want to store and keep using within the WHERE
clause is this DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)
, I removed the PHP variable because I recall can create confusion. 我想在WHERE
子句中存储和继续使用的数据是DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)
,我删除了PHP变量,因为我记得会造成混乱。
Is just that Giorni_reminder
is a result of the SELECT and I want to use it in WHERE clause. 仅仅是Giorni_reminder
是SELECT的结果,我想在WHERE子句中使用它。 If you see the query, I am repeating the same thing several times. 如果您看到查询,那么我将重复同一件事。
I wanted to avoid that. 我想避免这种情况。
So, something like this doesn't work for me: 因此,这样的事情对我不起作用:
SET @start = 1, @finish = 10;
I need something like posted in OP. 我需要在OP中发布的内容。 If it's possible . 如果可能的话 。
If it's not possible, then I have learned a lesson! 如果不可能,那我就学到了教训! :D :d
Editted-2 Editted-2
Please remove/update the definer below to match your database. 请删除/更新下面的定义器以匹配您的数据库。
CREATE DEFINER=`Connect7827`@`%` PROCEDURE `new_procedure`(iN UserIput varchar(1000))
BEGIN
declare vNewDate_Value varchar(100);
SET vNewDate_Value=concat("","DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)");
Set @vquery=concat_ws
("","INSERT INTO tab_reminder (ID_cliente, ID_articolo, Data_acquisto)
SELECT c.ID_cliente as cliente, ca.ID_articolo as articolo, MAX(c.Data_contratto) as ultimoAcquisto
FROM tab_contratti_articoli ca
LEFT JOIN tab_contratti c ON c.ID_contratto = ca.ID_contratto
LEFT JOIN tab_articoli a ON ca.ID_articolo = a.ID_articolo
LEFT JOIN tab_clienti cl ON c.ID_cliente = cl.ID_cliente
WHERE c.ID_cliente != 0
AND cl.ID_utente = 0
AND Giorni_reminder != 0
AND c.Data_contratto <= vnewDate
AND c.Data_contratto > DATE_SUB(vNewDate, INTERVAL 60 DAY)
AND (c.Data_contratto NOT BETWEEN vNewDate AND '$oggi')
GROUP BY c.ID_cliente, ca.ID_articolo
ON DUPLICATE KEY UPDATE ID_cliente=ID_cliente, ID_articolo=ID_articolo, Data_acquisto=Data_acquisto, Articolo_visibile=Articolo_visibile;");
Set @vquery= replace(@vquery,'$oggi','2017-05-16');
Set @vquery= replace(@vquery,'vNewDate',vNewDate_Value);
Select @vquery;
PREPARE stmt FROM @vquery;
EXECUTE stmt;
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.