gpt4 book ai didi

mysql - 如果值为 null 则忽略条件 MYSQL/Codeigniter

转载 作者:行者123 更新时间:2023-11-29 14:03:29 26 4
gpt4 key购买 nike

我正在使用 MYSQL/Codeigniter。如果传递的值为空,MySQL 查询如何忽略条件,如下例:

function get_category($category_id = 0){

return $this->db->query("SELECT * FROM {$this->table} c
INNER JOIN db_category_event ce
ON ce.category_id = c.category_id
INNER JOIN db_event_type e
ON e.event_id = ce.event_id
WHERE c.category_id = {$category_id}
WHERE c.visible = 1 AND e.visible = 1")
->result();
}

最佳答案

试试这个:

SELECT * 

FROM {$this->table} c

INNER JOIN db_category_event ce
ON ce.category_id = c.category_id

INNER JOIN db_event_type e
ON e.event_id = ce.event_id

WHERE ({$category_id} IS NULL OR c.category_id = {$category_id})
AND c.visible = 1 AND e.visible = 1

或者,如果参数被视为零,则这应该有效:

SELECT * 

FROM {$this->table} c

INNER JOIN db_category_event ce
ON ce.category_id = c.category_id

INNER JOIN db_event_type e
ON e.event_id = ce.event_id

WHERE ({$category_id} = 0 OR c.category_id = {$category_id})
AND c.visible = 1 AND e.visible = 1

关于mysql - 如果值为 null 则忽略条件 MYSQL/Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730627/

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