简体   繁体   English

OR WHERE IN 在带有子查询的 laravel 查询生成器中

[英]OR WHERE IN in laravel Query Builder with sub query

How can make query builder for following Query如何为以下查询制作查询构建器

SELECT * FROM `rad_nas` WHERE nas_assigned = ".$id." 
OR nas_assigned IN 
(select franchise_id from rad_franchise_has_parent where parent_id=".$id." )
OR nas_id IN 
(select  fra_nas_id  from rad_franchise_has_nas where franchise_nas_id=".$id.")

Check the docs for future uses and don't post a question and expect other people to write or debug your code for you, that is your job, we are giving you guidance, but here is a complete example when you transform that to Query Builder检查文档以供将来使用,不要发布问题并期望其他人为您编写或调试您的代码,这是您的工作,我们正在为您提供指导,但这里有一个完整的示例,当您将其转换为Query Builder

$results = DB::table('rad_nas')->where('nas_assigned', $id)
             ->orWhereIn('nas_assigned', function($query) use ($id) {
                  $query->select('franchise_id')
                        ->from('rad_franchise_has_parent')
                        ->where('parent_id', $id);
             }
             ->orWhereIn('nas_id', function($query) use ($id) {
                  $query->select('fra_nas_id')
                        ->from('rad_franchise_has_nas')
                        ->where('franchise_nas_id', $id);
             }

As @DanielO pointed out正如@DanielO 指出的那样

Raw statements will be injected into the query as strings, so you should be extremely careful to not create SQL injection vulnerabilities.原始语句将作为字符串注入到查询中,因此您应该非常小心,不要创建 SQL 注入漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM