php - 查询是否为空值
问题描述
当我有多个可以为空的变量时,是否有更好的方法来构造一个 mysql 查询来处理“为空”?
DB::connection('mysql')->select('select * from db where width = ? and height = ? and length = ? and color = ?', [$width, $height, $length, $color]);
代替
if ($width == null) {
DB::connection('mysql')->select('select * from db where width is null and height = ? and length = ? and color = ?',[$height, $length, $color]);
}elseif($height == null) {
...
解决方案
此答案可能不会使用您的表格。但我很久以前偶然发现了这一点。在我的示例中,我使用模型、列名和列值的数组。
数组:
$filterData = [
'width' => 21.0,
'height' => 11.5,
'length' => null
];
查询:
$mainQuery = Furniture::where($filterData);
要查看输出:$mainQuery->toSql();
推荐阅读
- speech-to-text - IBM Watson SpeechToText 标点符号
- java - log4j2 - StatusLogger 找不到 Log4j 2 配置文件。使用默认配置
- yahoo-api - 雅虎 OAuth 2.0 登录故障
- python - 从py创建exe在执行时默认传递参数?
- python - 设置图的宽度并从子图的纵横比推断高度
- ubuntu-18.04 - 错误处理包linux-image-4.15.0-20-generic
- javascript - String.replace() 在不同编码的情况下
- javascript - 如何在从桌面到平板电脑视图到移动视图的响应式页面上保持选中状态
- c# - 只读结构与类
- logstash - Graylog:糟糕的 GELF UDP 性能