当我尝试在触发器中使用多个分隔的ifs条件来测试字段是否不同于空值时,尝试计算表中的行数时遇到问题。
我试图将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!='')附近的语法错误 其中的名称类似于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 epic
和where 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 |
+----+------------+----------+
这不是我想要的,此解决方案的问题是它在我的表中搜索可以为空的任何数据。
提前感谢您的帮助
答案 0 :(得分: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;
毕竟'%%'作为模式都匹配。