繁体   English   中英

laravel查询中具有where条件的数组值

[英]array values with where condition in laravel query

我有一个名为assetIDs的数组,如下所示

$assetIDs = Collection {#505 ▼
  #items: array:2 [▼
    0 => 4
    1 => 7
  ]
}

而且我在表中的数据如下所示

数据

我正在使用此查询上表

$supplier_id = SupplierAsset::whereIn('asset_id',$asset_ids)->pluck('supplier_id');

以上查询的结果如下

Collection {#510 ▼
  #items: array:3 [▼
    0 => 1
    1 => 1
    2 => 2
  ]
}

这里whereIn返回满足条件的所有可能的行。 其实我需要的结果像这supplier_id既具有的价值观assetIDs array.In我的表supplier_id=1具有两值47就像下面集合。

Collection {#510 ▼
  #items: array:3 [▼
    0 => 1
    1 => 1
  ]
}

有人可以给我建议解决方案吗?

你可以试试:

$supplier_id = SupplierAsset::whereIn('asset_id',$asset_ids)
               ->groupBy('supplier_id')
               ->havingRaw('count(distinct asset_id) = ' . count($assetIDs))
               ->pluck('supplier_id');

这是您应该执行的mysql:

1-获取具有1个以上Supplier_id的所有ID:

SELECT supplier_id, Count(distinct asset_id) as dist_asst
 FROM Table
 GROUP BY supplier_id
 HAVING dist_asst > 1

2-然后加入:

SELECT t1.supplier_id
FROM Table t1
INNER JOIN (SELECT supplier_id, Count(distinct asset_id) as dist_asst
     FROM Table
     GROUP BY supplier_id
     HAVING dist_asst > 1) t2 on t2.supplier_id = t1.supplier_id

暂无
暂无

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

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