dongyuan9109 2018-06-22 09:41
浏览 156
已采纳

如何在Laravel查询构建器中编写子查询

i have an mssql query that looks like this:

SELECT * FROM (

    SELECT a.Client, ad.Kto, a.Address, a.Matchcode, a.Name1, a.country, a.ZIP, a.Street FROM [HeadQuarter].[dbo].[Addresses] a
            INNER JOIN [HeadQuarter].[dbo].[AddressesDetails] ad ON (a.Client = ad.Client AND 
                                                                 a.Address = ad.Address)

    WHERE ad.Active <> 0 AND
          (a.USER_emailactive = 0 OR a.USER_emailactive IS NULL)

) client_id

WHERE (a.Country = 'AT' AND a.ZIP BETWEEN '0000' AND '5000')

i converted the inner select into Laravel query builder

\DB::connection('sqlsrv')
->table('[HeadQuarter].[dbo].[Addresses]')
->join([HeadQuarter].[dbo].[AddressesDetails], function($join){

$join->on('[HeadQuarter].[dbo].[Addresses].Client', '=', '[HeadQuarter].[dbo].[AddressesDetails].Client')
     ->on('[HeadQuarter].[dbo].[Addresses].Address', '=', '[HeadQuarter].[dbo].[AddressesDetails].Address')

})
->select('[HeadQuarter].[dbo].[Addresses].Client, [HeadQuarter].[dbo].[AddressesDetails].Kto,....')
->where('[HeadQuarter].[dbo].[AddressesDetails].Active', '<>', '0')
->whereRaw('(a.USER_emailactive = 0 OR a.USER_emailactive IS NULL)')
->get();

and this is working. But now how can i get the

SELECT * FROM (..inner query..) client_id 
WHERE (a.Country = 'AT' AND a.ZIP BETWEEN '0000' AND '5000')

convert to my query builder. sure i could use ->select() and write the raw sql query but i need this in the query builder because my inner and outer where clause i optional

  • 写回答

1条回答 默认 最新

  • dqq46733 2018-06-22 10:10
    关注

    I guess you could simplify your query as below, there is not need for sub query

    SELECT a.Client, ad.Kto, a.Address, a.Matchcode, a.Name1, a.country, a.ZIP, a.Street 
    FROM [HeadQuarter].[dbo].[Addresses] a
    INNER JOIN [HeadQuarter].[dbo].[AddressesDetails] ad 
    ON (a.Client = ad.Client AND a.Address = ad.Address)
    WHERE ad.Active <> 0 
        AND a.Country = 'AT' 
        AND a.ZIP BETWEEN '0000' AND '5000'
        AND (a.USER_emailactive = 0 OR a.USER_emailactive IS NULL)
    

    In query builder you can use Parameter Grouping

    \DB::connection('sqlsrv')
        ->table('[HeadQuarter].[dbo].[Addresses] as a')
        ->join('[HeadQuarter].[dbo].[AddressesDetails] as b', function($join){
            $join->on('a.Client', '=', 'ad.Client')
                 ->on('a.Address', '=', 'ad.Address');
    
        })
        ->select('a.Client', 'ad.Kto', 'a.Address', 'a.Matchcode', 'a.Name1', 'a.country', 'a.ZIP', 'a.Street')
        ->where('ad.Active', '<>', '0')
        ->where(function ($query) {
            $query->whereNull('a.USER_emailactive')
                  ->orWhere('a.USER_emailactive', '=', '0');
         })
         ->where(function ($query) {
            $query->orWhere(function ($query) {
                $query->where('a.Country', '<>', 'AT')
                      ->whereBetween('a.ZIP', ['0000', '5000']);
            })->orWhere(function ($query) {
                $query->where('a.Country', '<>', 'Foo')
                      ->whereBetween('a.ZIP', ['0000', '5000']);
            })->orWhere(function ($query) {
                $query->where('a.Country', '<>', 'Bar')
                      ->whereBetween('a.ZIP', ['0000', '5000']);
            });
        })
        ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包