在 WherenotIn 中使用 Laravel 查询生成器
Use Laravel query builder inside WherenotIn
我有以下查询,我想知道这在 laravel 的查询生成器中是否可行:
SELECT "a".*
FROM "area" AS "a"
INNER JOIN "transaction" AS "t" ON "t"."f_id" = "a"."f_id"
WHERE "t"."id" = 'c5931409-37b7-4248-b958-831edaae4075'
AND "a"."id" NOT IN (
SELECT "zc"."a_id"
FROM "transaction_detail" AS td
INNER JOIN "zone_component" AS "zc" ON "zc"."id" = "td"."comp_id"
WHERE td."t_id" = 'c5931409-37b7-4248-b958-831edaae4075'
AND td."card_type" IN ( 'C_OVA', 'C_M21' )
);
您可以传递一个 Closure
对象或 Builder
对象作为 whereIn
/whereNotIn
的第二个参数
// Builder
$sub = DB::query()
->select('za.aid') // SELECT "zc"."a_id"
->from('transaction_detail', 'td') // FROM "transaction_detail" AS td
->join('zone_component as zc', 'zc.id', 'td.comp_id') // INNER JOIN "zone_component" AS "zc" ON "zc"."id" = "td"."comp_id"
->where('td.t_id', 'c5931409-37b7-4248-b958-831edaae4075') // WHERE td."t_id" = 'c5931409-37b7-4248-b958-831edaae4075'
->whereIn('td.card_type', ['C_OVA', 'C_M21']); // AND td."card_type" IN ( 'C_OVA', 'C_M21' )
// Closure
$sub = function ($query) {
$query->select('za.aid') // SELECT "zc"."a_id"
->from('transaction_detail', 'td') // FROM "transaction_detail" AS td
->join('zone_component as zc', 'zc.id', 'td.comp_id') // INNER JOIN "zone_component" AS "zc" ON "zc"."id" = "td"."comp_id"
->where('td.t_id', 'c5931409-37b7-4248-b958-831edaae4075') // WHERE td."t_id" = 'c5931409-37b7-4248-b958-831edaae4075'
->whereIn('td.card_type', ['C_OVA', 'C_M21']); // AND td."card_type" IN ( 'C_OVA', 'C_M21' )
};
// Full Query
$results = DB::query()
->select('a.*') // SELECT "a".*
->from('area', 'a') // FROM "area" AS "a"
->join('transaction as t', 't.f_id', 'a.f_id') // INNER JOIN "transaction" AS "t" ON "t"."f_id" = "a"."f_id"
->where('t.id', 'c5931409-37b7-4248-b958-831edaae4075') // WHERE "t"."id" = 'c5931409-37b7-4248-b958-831edaae4075'
->whereNotIn('a.id', $sub) // AND "a"."id" NOT IN ( ... )
->get();
您也可以内联子查询,而不是将它们作为单独的变量。
whereNotIn('a.id', DB::query()->.....)
whereNotIn('a.id', function ($query) { .... })
我有以下查询,我想知道这在 laravel 的查询生成器中是否可行:
SELECT "a".*
FROM "area" AS "a"
INNER JOIN "transaction" AS "t" ON "t"."f_id" = "a"."f_id"
WHERE "t"."id" = 'c5931409-37b7-4248-b958-831edaae4075'
AND "a"."id" NOT IN (
SELECT "zc"."a_id"
FROM "transaction_detail" AS td
INNER JOIN "zone_component" AS "zc" ON "zc"."id" = "td"."comp_id"
WHERE td."t_id" = 'c5931409-37b7-4248-b958-831edaae4075'
AND td."card_type" IN ( 'C_OVA', 'C_M21' )
);
您可以传递一个 Closure
对象或 Builder
对象作为 whereIn
/whereNotIn
// Builder
$sub = DB::query()
->select('za.aid') // SELECT "zc"."a_id"
->from('transaction_detail', 'td') // FROM "transaction_detail" AS td
->join('zone_component as zc', 'zc.id', 'td.comp_id') // INNER JOIN "zone_component" AS "zc" ON "zc"."id" = "td"."comp_id"
->where('td.t_id', 'c5931409-37b7-4248-b958-831edaae4075') // WHERE td."t_id" = 'c5931409-37b7-4248-b958-831edaae4075'
->whereIn('td.card_type', ['C_OVA', 'C_M21']); // AND td."card_type" IN ( 'C_OVA', 'C_M21' )
// Closure
$sub = function ($query) {
$query->select('za.aid') // SELECT "zc"."a_id"
->from('transaction_detail', 'td') // FROM "transaction_detail" AS td
->join('zone_component as zc', 'zc.id', 'td.comp_id') // INNER JOIN "zone_component" AS "zc" ON "zc"."id" = "td"."comp_id"
->where('td.t_id', 'c5931409-37b7-4248-b958-831edaae4075') // WHERE td."t_id" = 'c5931409-37b7-4248-b958-831edaae4075'
->whereIn('td.card_type', ['C_OVA', 'C_M21']); // AND td."card_type" IN ( 'C_OVA', 'C_M21' )
};
// Full Query
$results = DB::query()
->select('a.*') // SELECT "a".*
->from('area', 'a') // FROM "area" AS "a"
->join('transaction as t', 't.f_id', 'a.f_id') // INNER JOIN "transaction" AS "t" ON "t"."f_id" = "a"."f_id"
->where('t.id', 'c5931409-37b7-4248-b958-831edaae4075') // WHERE "t"."id" = 'c5931409-37b7-4248-b958-831edaae4075'
->whereNotIn('a.id', $sub) // AND "a"."id" NOT IN ( ... )
->get();
您也可以内联子查询,而不是将它们作为单独的变量。
whereNotIn('a.id', DB::query()->.....)
whereNotIn('a.id', function ($query) { .... })