gpt4 book ai didi

mysql - 将 "ifs"嵌套在 select-where 中以进行条件动态过滤

转载 作者:行者123 更新时间:2023-11-29 15:59:32 25 4
gpt4 key购买 nike

最近我创建了一个基于 codeigniter 3-php 和 PHPMyADMIN-mysql 的新网站,我集成的分页比使用 ajax 进行的动态搜索还要多。我的问题是,当我有超过 100 万个 50 万个结果时,查询开始花费大量时间。

对于这个解决方案,我考虑使用触发器。所以我在使用触发器时遇到了一点麻烦,就是当我尝试制作动态过滤器时,我举了一个例子来更好地理解,我想知道存储在另一个动态表中的过滤器对应的行数。

BEGIN
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

问题是,如果我删除所有 ifs 语句 条件,我的过滤器将无法工作,如果我的过滤器包含空字段,它会将结果破坏为 0,我希望它是否为空字段它不算作过滤的参数。我也尝试过案例陈述,这也没有帮助。

当我运行代码时,我得到的错误是:

MySQL replied: # 1064 - Syntax error near 'IF' (new.client! = '') THEN WHERE name LIKE CONCAT ('%', NEW.client, '%') END IF;

有一个很长的解决方案可以工作,但我不需要,因为我不能将其称为解决方案:
就是把外面的取出来,对得到的每一个case进行select,换句话说:

if (client! = 0 and common == 0 and type == 0 and number == 0 and tare == 0) then
{filter by client only}
otherwise-if (client! = 0 and common! = 0 and type == 0 and number == 0 and tare == 0) then
{filter by client and common only} .... ect

我将尝试举一个例子来使其更清楚:这是我的历史表:

|                                         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 |
+----+-------------+---------+--------+--------------+-------+---------+---------+

我想检索包含 cancled = 0 的行数,并且在其名称中包含 EPIC

所以我的文件管理器数组将是这样的:

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

结果我应该有这样的东西

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

如果它有帮助,我会将我的 php 代码放在那里,该代码可以工作,但加载结果需要太多时间


$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();

最佳答案

您是否正在寻找加入分组依据

select f.id,
count(*) as num_rows,
sum(tare) as sum_tare
from history h join
temp_fetch f
on h.nom like concat('%', f.client, '%') or
h.commune like concat('%', f.commune, '%') or
h.type like concat('%', f.type, '%') or
h.mat like concat('%', f.matricule, '%') or
h.tare like concat('%', f.tare, '%')
where f.cancled = 0
group by f.id;

关于mysql - 将 "ifs"嵌套在 select-where 中以进行条件动态过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56290552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com