laravel - 从表中选择(laravel) - where 和 orWhere
问题描述
所以,我正在使用 laravel,我想让这个查询在 laravel 'formula' 中工作
SELECT * FROM `complaints` WHERE (`fID` = 0) AND (`hide_topic` = 0 OR (`hide_topic` <= 0 OR (`hide_topic` > 3 AND byID = 52))) ORDER BY `status` ASC, `id` DESC
我在 laravel 中做了类似的事情,但不确定是否像上面的查询那样正确选择。
DB::table('complaints')
->where('fID', '=', 0)
->where('hide_topic', '=', 0)
->orWhere('hide_topic', '<', 0)
->orWhere('hide_topic', '>', 3)
->where('byID', '=', 52)
->orderBy('status', 'asc')
->orderBy('id', 'desc')
->get();
有什么帮助吗?
解决方案
您只需将where 与 clouser 一起用于每个复合条件:
$admin_level=3;
$playerid=52;
DB::table('complaints')->where('fID', '=', 0)
->where(function ($query)use($admin_level,$playerid){
$query->where('hide_topic', '=', 0)->orWhere(function ($query)use($admin_level,$playerid){
$query->Where('hide_topic', '<', 0)->orWhere(function ($query)use($admin_level,$playerid){
$query ->Where('hide_topic', '>', $admin_level)
->where('byID', '=', $playerid);
});
});
})
->orderBy('status', 'asc')
->orderBy('id', 'desc')
->get();
推荐阅读
- docker - 如何配置 Nginx 容器以使用自签名证书为在主机上运行的应用程序提供服务?
- javascript - 用另一个 DOM 完全替换 HTML DOM
- python - Selenium 无法在 Twitter 注册页面中找到名称字段
- wordpress - Wordpress/Pardot/Contact Form 7(需要验证号码和电子邮件的插件)
- jpeg - 为什么 libjpeg 不正确地存储我的图像数组?
- pdf - 如何使用Java iText在pdf文件中将字体名称作为字符串传递
- python - 根据常量 python 列表设置张量值的优雅方法
- python - 在 Django 中使用 python 创建本地文件
- java - 如何定义 JTable 的 Column 的数据类型与 MySql 数据库相同?
- c# - SetTextCharacterExtra 函数不适用于特定字体/字体大小的 DrawText 函数