mysql - 在 where 子句中使用两组条件
问题描述
我正在尝试获取表中应该满足这些条件的项目数
status = active
type = Pre-order
date = $date_input
OR
status = active
type = Both
date = $date_input
我正在尝试使用此声明,但我很确定它搞砸了。
SELECT COUNT(id) as count_date from date_restriction
where (date='$date_input' AND status='active' AND type='Pre-order')
OR (date='$date_input' AND status='active' AND type='Both')
我也试过这个无济于事
SELECT COUNT(id) as count_date from date_restriction
where date='$date_input' AND status='active' AND type='Pre-order' OR type='Both'
解决方案
如果您混合了 AND 和 OR 条件,则 or 子句需要 ()
SELECT COUNT(id) as count_date
from date_restriction
where date='$date_input'
AND status='active'
AND ( type='Pre-order' OR type='Both')
或几个或条件你可以使用 IN 子句
AND type IN ('Pre-order', 'Both')
无论如何,你应该避免在 SQL 中使用 php var 你有 sqlinjection 的风险 .. 为了避免这种情况,你应该看看你的数据库驱动程序的准备好的语句和绑定参数
推荐阅读
- php - PHP preg_replace 删除方括号之间的所有点字符
- c++ - 使用基于范围的 for 循环填充指针向量
- java - 在 xml 中使 ConcurrentMapCacheManager 事务感知
- c++ - 禁止复制构造函数和赋值运算符单例类
- mysql - Codeigniter 活动记录返回指定行的列名
- python - 数据框中的条形图
- java - 将Java中的对象列表序列化为json
- r - R Leaflet Shiny:shape_click$id 为 NULL
- jenkins - 如何在 Jenkinsfile 中捕获分支名称
- list - aVdin 8 中的样式列表选择