【问题标题】:Declare variables MySQL声明变量 MySQL
【发布时间】:2018-10-31 16:42:52
【问题描述】:

我正在尝试声明变量并像这样运行查询:

$sql = "SELECT @dateconsult := (YEARWEEK('2018-10-01',3)),
               @countunits  := ( SELECT COUNT(s.id_production)
                                 FROM sw_sowing
                                 WHERE status != 0
                                 AND YEARWEEK(date,3) <= @dateconsult
                                 GROUP BY id_production_unit_detail
                               ),
               @quadrants   := ( SELECT DISTINCT value
                                 FROM cf_config
                                 WHERE parameter = 'PLANTHEALTH'
                               );

       SELECT FORMAT(((count_quadrant * 100)/(total_units * Cuadrantes)),3) AS incidence
       FROM (
             SELECT @countunits AS total_units, @quadrants AS Cuadrantes,
             FROM ph_planthealth
             INNER JOIN ph_planthealth_detail ON ph_planthealth_detail.id_p = ph_planthealth.id
             WHERE YEARWEEK(ph_planthealth.date,3) = @dateconsult
             AND ph_planthealth.status = 200
             AND ph_planthealth.id_tenant = 1
             AND ph_planthealth_detail.id_plague != 0
             GROUP BY ph_planthealth_detail.id_plague
      ) AS s
      ORDER BY incidence DESC; ";

    $plague = $this->db->fetchAll($sql, Phalcon\Db::FETCH_ASSOC, $options) ";

问题在于它显示了第一个 SELECT 的结果,这是我声明的变量,而不是作为主查询的第二个 SELECT 的结果。

第一次声明变量,不知道做对了没有。

感谢您的 cmets 和有关此主题的帮助。

【问题讨论】:

  • fetchAll() 一次只能返回一个查询的结果。您应该在自己的调用中执行第二个SELECT
  • also ` $plague = $this->db->fetchAll($sql, Phalcon\Db::FETCH_ASSOC, $options) ";` 你有一个结束 " 在结尾分号前的最后一行

标签: php mysql variables phalcon


【解决方案1】:

您无需在单独的SELECT 中进行变量赋值。您可以通过加入主查询来完成。

SELECT FORMAT(((count_quadrant * 100)/(total_units * Cuadrantes)),3) AS incidence
FROM (
     SELECT @countunits AS total_units, @quadrants AS Cuadrantes,
     FROM ph_planthealth
     INNER JOIN ph_planthealth_detail ON ph_planthealth_detail.id_p = ph_planthealth.id
     WHERE YEARWEEK(ph_planthealth.date,3) = @dateconsult
     AND ph_planthealth.status = 200
     AND ph_planthealth.id_tenant = 1
     AND ph_planthealth_detail.id_plague != 0
     GROUP BY ph_planthealth_detail.id_plague
) AS s
CROSS JOIN (
    SELECT @dateconsult := (YEARWEEK('2018-10-01',3)),
           @countunits  := ( SELECT COUNT(s.id_production)
                             FROM sw_sowing
                             WHERE status != 0
                             AND YEARWEEK(date,3) <= @dateconsult
                             GROUP BY id_production_unit_detail
                           ),
           @quadrants   := ( SELECT DISTINCT value
                             FROM cf_config
                             WHERE parameter = 'PLANTHEALTH'
                           )
) AS vars
ORDER BY incidence DESC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多