出于某些奇怪的原因,我的查询中的In条件下这些参数不起作用。
如果我对参数进行硬编码,查询将返回结果:
DB::select("SELECT * FROM my_table WHERE vendor_id IN (19009, 19010)
这些不会返回结果:
DB::select("SELECT * FROM my_table WHERE vendor_id IN (':vendor_ids')", [
':vendor_ids' => implode([
19010,
19009,
]),
]);
DB::select("SELECT * FROM my_table WHERE vendor_id IN (':vendor_ids')", [
':vendor_ids' => [
19010,
19009,
],
]);
DB::select("SELECT * FROM my_table WHERE vendor_id IN (':vendor_ids')", [
':vendor_ids' => '19009, 19010',
]);
答案 0 :(得分:1)
使用const obj = {
"data": [{
"order_id": "336566",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-21 15:00:27",
"delivery_date": "2019-01-24",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "335719",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2018-12-24 13:46:27",
"delivery_date": "2018-12-30",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "336531",
"customer_name": "Tay",
"customer_surname": "Z",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-11 08:42:27",
"delivery_date": "2019-01-17",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "336545",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-17 19:00:27",
"delivery_date": "2019-01-18",
"delivery_from": "11:00",
"delivery_to": "12:00",
"completed": "0",
"shopper": "joel"
}, {
"order_id": "241918",
"customer_name": "Marietjie",
"customer_surname": "Short",
"total_items": "44",
"completed_items": "7",
"percent_complete": 0.15909090909091,
"datetime_received": "2018-07-25 15:18:25",
"delivery_date": "2018-10-29",
"delivery_from": "12:00",
"delivery_to": "13:00",
"completed": "0",
"shopper": "Tay"
}, {
"order_id": "281774",
"customer_name": "Ashleigh",
"customer_surname": "Hodge",
"total_items": "16",
"completed_items": "0",
"percent_complete": 0,
"datetime_received": "2018-10-04 15:59:19",
"delivery_date": "2018-10-29",
"delivery_from": "12:00",
"delivery_to": "13:00",
"completed": "0",
"shopper": null
}, {
"order_id": "336544",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-17 08:00:27",
"delivery_date": "2019-01-19",
"delivery_from": "13:00",
"delivery_to": "14:00",
"completed": "0",
"shopper": "joel"
}]
}
//Length
console.log(obj.data.length)
//Child Item
console.log(obj.data[3].order_id)
仅返回一行。
您需要分别绑定参数,例如IN(:paramters)
。
因此,如果执行以下操作,您应该会得到预期的结果
IN(:parameter1, parameter2)
要动态生成参数,如果有供应商ID遍历它们,并创建另一个关联数组
DB::select("SELECT * FROM my_table WHERE vendor_id IN (':vendor_id1, :vendor_id2',...)",
[':vendor_id1' => 1, ':vendor_id2' => 2]);
然后
$vendors = [];
foreach ($vendorId as $id) {
$vendors[':vendor' . $id] = $id;
}
答案 1 :(得分:0)
Laravel有一个非常不错的query builder
尝试一下:
$resultCollection = DB::table('my_table')
->select('*')
->whereIn('vendor_id', [19009, 19010])
->get();
如果您确实要使用原始查询,请尝试DB::raw
像这样:
$resultCollection = DB::table('my_table')
->whereRaw('vendor_id IN (?)', [19010,19009])
->get();