MySQL - SELECT语句

时间:2017-05-16 09:07:58

标签: php mysql

我有这个问题:

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;

基本上,我不得不重复DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY)日期结果。

有没有办法在MySQL中声明该日期的变量?

类似的东西:

SET @newDate := DATE_SUB(DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY);

然后像这样使用它:

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')

我已经搜索过并找到了我发现的内容,但是没有办法或者它不允许在WHERE子句中声明变量或别名,但也许有一种解决方法可以避免输入相同内容在同一个查询中多次出现这种情况。

一如既往,谢谢大家,祝你们度过愉快的一天!

更新1:

我已经看过@ TimBiegeleisen的链接,我发现它非常非常有趣。但我工作的项目使用不推荐使用的mysql API进行查询。见例:

$sqlExample = "SELECT * FROM tab_example";
$qExample = mysql_query($sqlExample) or die ("Error in $sqlExample: ".mysql_error());
$rsExample = mysql_fetch_object($qExample);

考虑到这一点,仍然Prepared statements可行吗?

返回OP,它是一种在我需要它的查询中声明变量的方法吗(在WHERE子句中)

更新2:

这是对原因的澄清:How to declare a variable in MySQL?

不适合我或我无法使其发挥作用。

我不必声明静态数据,我想声明一个由SELECT产生的变量。

我想要在WHERE子句中存储并继续使用的数据是DATE_SUB('2017-05-16', INTERVAL Giorni_reminder DAY),我删除了PHP变量,因为我记得会造成混淆。

只是Giorni_reminder是SELECT的结果,我想在WHERE子句中使用它。如果您看到查询,我会重复几次相同的事情。

我想避免这种情况。

所以,这样的事情并不适合我:

SET @start = 1, @finish = 10;  

我需要在OP中发布的内容。 如果可能

如果不可能,那我就吸取了教训! :d

1 个答案:

答案 0 :(得分:0)

<强> Editted-2
请删除/更新下面的定义器以匹配您的数据库。

  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