在Laravel Eloquent查询构建器中将UNION与DISTINCT配合使用

时间:2019-03-04 15:24:31

标签: laravel postgresql eloquent distinct union

我有2张桌子

properties
+----+-----------+
| id | parent_id |
+----+-----------+
|  1 | null      |
|  2 | 1         |
|  3 | null      |
|  4 | 3         |
|  5 | 3         |
|  6 | null      |
+----+-----------+

sale_services
+----+-------------+------+
| id | property_id | rank |
+----+-------------+------+
|  1 |           2 |    5 |
|  2 |           4 |    4 |
|  3 |           5 |    6 |
|  4 |           6 |    7 |
+----+-------------+------+

以及通过关系( sale_service.property_id = property.id )相互链接的相应口才模型( SaleService 属性)。可以将属性链接到同一表中的另一个属性

我需要获取 SaleService 实例的集合,其中相关的property.parent_id为空,或者如果sale_services表中有一些记录通过以下方式共享相同的parent_id properties表,由该字段和顺序区分为rank

结果应该是

+----+-------------+------+
| id | property_id | rank |
+----+-------------+------+
|  1 |           2 |    5 |
|  3 |           5 |    6 |
|  4 |           6 |    7 |
+----+-------------+------+

-sale_services表中除(sale_service.id = 2)之外的所有项目,因为它的属性与项目(sale_service.id = 3)共享parent_id,且具有(sale_service.id = 3)的商品具有最高rank

我想出了SQL代码以获得理想的结果,

 SELECT *
    FROM
      (SELECT DISTINCT ON (properties.parent_id) *
       FROM "sale_services"
       INNER JOIN "properties" ON "sale_services"."property_id" = "properties"."id"
       WHERE ("properties"."parent_id") IS NOT NULL
       ORDER BY "properties"."parent_id", "sale_services"."rank" DESC) AS sub
    UNION
    SELECT *
    FROM "sale_services"
    INNER JOIN "properties" ON "sale_services"."property_id" = "properties"."id"
    WHERE ("properties"."parent_id") IS NULL

但是我无法通过Eloquent Builder达到同样的效果。

我尝试过这样的事情

$queryWithParent = SaleService::query()
    ->select(\DB::raw('DISTINCT ON (properties.parent_id) *'))
    ->whereNotNull('properties.parent_id')
    ->join('properties', 'sale_services.property_id', '=', 'properties.id')
    ->orderBy('parent_id')
    ->orderBy('sale_services.index_range', 'desc');

$queryWithoutParent = SaleService::query()
    ->join('properties', 'sale_services.property_id', '=', 'properties.id')
    ->whereNull('properties.parent_id');

$query = $queryWithParent->union($queryWithoutParent);

但出现错误

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "union" LINE 1: ...perties.type <> 'hotel') order by "parent_id" asc union sele... ^ (SQL: select DISTINCT ON (properties.parent_id) * from "sale_services" inner join "properties" on "sale_services"."property_id" = "properties"."id" where ("properties"."parent_id") is not null and ("sale_services"."deleted_at") is null and "published" = 1 and exists (select 1 from "properties" where properties.id = sale_services.property_id AND properties.type <> 'hotel') order by "parent_id" asc union select * from "sale_services" inner join "properties" on "sale_services"."property_id" = "properties"."id" where ("properties"."parent_id") is null and ("sale_services"."deleted_at") is null and "published" = 1 and exists (select 1 from "properties" where properties.id = sale_services.property_id AND properties.type <> 'hotel') order by "index_range" desc limit 12 offset 0)

如果我从第一个查询( $ queryWithParent )中删除排序,它似乎可以工作,但是在不同的查询中选择了随机项目。

还有其他方法可以达到相同的结果,还是我做错了?

2 个答案:

答案 0 :(得分:0)

“ Distinct”的laravel查询创建者是distinct(),如:

count = 0
while len(player1) != 52 and len(player2) != 52:
    hand1 = player1[count]
    hand2 = player2[count]
    val = cardValue[count]

    (now I need to do something)

    count = count + 1

那行得通吗?

答案 1 :(得分:0)

最后找到了解决方案。

  1. 使用Laravel ^ 5.7.28(Laravel 5.7.28中已解决UNION问题:github.com/laravel/framework/pull/27589),您将不需要子查询!
  2. 指定要选择的表列,以避免列名冲突sale_services.*

    $queryWithParent = SaleService::query()
        ->select(\DB::raw('DISTINCT ON (properties.parent_id) sale_services.*'))
        ->from('sale_services')
        ->join('properties', 'sale_services.property_id', '=', 'properties.id')
        ->whereNotNull('properties.parent_id')
        ->orderBy('properties.parent_id')
        ->orderBy('sale_services.index_range', 'desc');
    
    $queryWithoutParent = SaleService::query()
        ->select(\DB::raw('sale_services.*'))
        ->join('properties', 'sale_services.property_id', '=', 'properties.id')
        ->whereNull('properties.parent_id');
    
    $query = $queryWithParent->union($queryWithoutParent);