【问题标题】:Multiple IFs for different conditional selections inside a trigger触发器内不同条件选择的多个 IF
【发布时间】:2019-10-09 19:32:02
【问题描述】:

当我尝试计算表中的行数时遇到问题,在触发器中使用多个分隔的 ifs 条件来测试字段是否不同于空。

我试图从 select 函数中取出 if 并且它可以工作,但我无法针对我拥有的所有情况都这样做,它不会被优化。

UPDATE max_tare
SET max_row = (
SELECT COUNT(*)
FROM history 
IF(NEW.client != '') THEN
WHERE nom LIKE CONCAT('%', NEW.client, '%') END IF;

IF(NEW.commune != '') THEN
WHERE commune LIKE CONCAT('%', NEW.commune, '%') END IF;

IF(NEW.type != '') THEN
WHERE type LIKE CONCAT('%', NEW.type, '%') END IF;

IF(NEW.matricule != '') THEN
WHERE mat LIKE CONCAT('%', NEW.matricule, '%') END IF;

IF(NEW.tare != '') THEN
WHERE tare LIKE CONCAT('%', NEW.tare, '%') END IF;

WHERE cancled = 0), 
    max_tare =
    (SELECT SUM(tare)
     FROM history
     WHERE cancled = 0) WHERE id = 1;

END

在 php 查询中,我很容易得到行号,但是当我尝试触发器时,我得到错误提示:

MySQL 回复:# 1064 - 'IF' (new.client! = '') THEN 附近的语法错误 WHERE name LIKE CONCAT ('%', NEW.client, '%') END IF;

我会借用在codeigniter框架下使用的php代码完美地工作,

$this->db->select("*");

$this->db->from('history');

if ($query[1] != '') {
    $this->db->like('nom', $query[1]);
                }
if ($query[2] != '') {
    $this->db->like('commune', $query[2]);
                }
if ($query[3] != '') {
    $this->db->like('type', $query[3]);
                }
if ($query[4] != '') {
    $this->db->like('mat', $query[4]);
                }
if ($query[10] != '') {
    $this->db->like('rfid', $query[10]);
                }
if ($query[5] != '') {
    $this->db->like('tare', $query[5]);
                }
if ($query[6] != '') {
    $this->db->where('date >', $query[6]);
                }
if ($query[7] != '') {
    $this->db->where('date <', $query[7]);
                }
if ($query[11] != '') {
    $this->db->where('time_plode >', $query[11]);
                }
if ($query[12] != '') {
    $this->db->where('time_plode <', $query[12]);
                }
$this->db->where('cancled', 0);
return $this->db->count_all_results();

此代码将返回行数,它在 php 下工作正常,但我想在触发器而不是 php 查询中使用它。

希望我更清楚

我会尝试做一个例子:

+----+--------+---------+------+--------------+------+
|                     temp_fetch                     |
+----+--------+---------+------+--------------+------+
| id | client | commune | type | matricule    | tare |
+----+--------+---------+------+--------------+------+
| 1  | EPIC   |         |      |              |      |
+----+--------+---------+------+--------------+------+

这是我的触发表,当我更新它时将激活它,让我们假设它已在此处更新为这种情况 现在我将尝试过滤我的结果以计算它们

+----+-------------+---------+--------+--------------+-------+---------+---------+
|                                      history                                   |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| id | nom         | commune | type   | mat          | tare  | rfid    | cancled |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 1  | EPIC paris  | france  | white  | 01248-816-16 | 7600  | ABCF44C | 0       |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 2  | EPIC london | UK      | white  | 06854-315-16 | 5233  | A8CG27C | 1       |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 3  | NET barça   | ESP     | red    | 03254-615-16 | 8900  | HBC54AC | 1       |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 4  | NET Dubai   | arab    | blue   | 35251-117-16 | 11200 | HDK7BV5 | 0       |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 5  | EPIC roma   | ita     | red    | 36524-618-16 | 7300  | NBL53DC | 0       |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 6  | SNC beta    | alpha   | green  | 69358-117-16 | 5400  | JDLF8ND | 1       |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 7  | EPIC tokyo  | japan   | yellow | 46258-712-16 | 8700  | K5ND55D | 1       |
+----+-------------+---------+--------+--------------+-------+---------+---------+
| 8  | SARL Fit    | body    | black  | 69531-614-16 | 9600  | AIES5HJ | 0       |
+----+-------------+---------+--------+--------------+-------+---------+---------+

所以这是我的表,我想计算与我的数组表匹配的行(第一个) 所以我在我的表 max_tare 中想要的结果:

+----------------------------+
|          max_tare          |
+----------------------------+
| id |  max_row   | max_tare |
+----+------------+----------+
| 1  |     2      | 14900    |
+----+------------+----------+

计算两行他们有nom like epicwhere cancled = 0 希望现在很清楚,因为我添加了简单的数据

当我尝试Danblack 解决方案时

UPDATE max_tare
SET max_row =
  (SELECT COUNT(*)
   FROM history
   WHERE nom LIKE CONCAT('%', NEW.client, '%')
     AND commune LIKE CONCAT('%', NEW.commune, '%')
     AND TYPE LIKE CONCAT('%', NEW.type, '%')
     AND mat LIKE CONCAT('%', NEW.matricule, '%')
     AND tare LIKE CONCAT('%', NEW.tare, '%')
     AND cancled = 0),
    max_tare =
  (SELECT SUM(tare)
   FROM history
   WHERE cancled = 0)
WHERE id = 1;

我不明白:

+----------------------------+
|          max_tare          |
+----------------------------+
| id |  max_row   | max_tare |
+----+------------+----------+
| 1  |     0      | 14900    |
+----+------------+----------+

这不是我想要的,这个解决方案的问题是它在我的表中搜索任何可以为空的数据。

提前感谢您的帮助

【问题讨论】:

  • != 应该在一起,没有空格(如果你已经正确粘贴了这个错误信息)
  • 很多地方的语法不正确。请阅读文档。
  • @MichaelO。它们在一起没有空格,我说我在不同的其他代码中使用了这种语法,它工作得很好,但在这种情况下,我不知道为什么:'(
  • @Hermanto 你在哪里看到导致问题的空格。
  • 样本数据、期望的结果以及对逻辑的解释都会有所帮助。

标签: mysql sql triggers mysql-error-1064


【解决方案1】:

首先,有一个IF 函数和一个IF 语句。它们看起来不同,并且与 SQL 语法的不同部分相关。

如果不考虑 if 语句中的空字符串而只使用:

UPDATE max_tare
SET max_row =
  (SELECT COUNT(*)
   FROM history
   WHERE nom LIKE CONCAT('%', NEW.client, '%')
     AND commune LIKE CONCAT('%', NEW.commune, '%')
     AND TYPE LIKE CONCAT('%', NEW.type, '%')
     AND mat LIKE CONCAT('%', NEW.matricule, '%')
     AND tare LIKE CONCAT('%', NEW.tare, '%')
     AND cancled = 0),
    max_tare =
  (SELECT SUM(tare)
   FROM history
   WHERE cancled = 0)
WHERE id = 1;

毕竟 '%%' 作为模式匹配所有。

【讨论】:

  • AND WHERE cancled = 0 应该是 AND cancled = 0
  • @danblack 感谢您的快速回答,但我已经尝试过了,当我尝试在不考虑空字段的情况下进行计数时,它会发送 0 行作为结果,因为它们都不包含空字符串:' (.
  • 你编造的语法和缺乏清晰的解释几乎没有留下你想要的结果的线索。我猜。错了。请使用一些示例数据和预期结果编辑问题
  • @danblack 我真正想要的是,只有当它不同然后为空时才使用一个字段,例如:如果 NEW.commune 为空,它将不会被执行,它将通过下一个.我可以将我的 php 代码放在 codeigniter 中吗?这可能有助于更清晰。
  • @phpbegginernoob 。 . .这段代码看起来很合理,可能是你想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
相关资源
最近更新 更多