我雄辩的查询构建器实例返回空,而 sql 子句返回结果。会欣赏第二只眼睛
Posted
技术标签:
【中文标题】我雄辩的查询构建器实例返回空,而 sql 子句返回结果。会欣赏第二只眼睛【英文标题】:My eloquent query builder instance is returning empty while the sql clause returns results. would appreciate a second eye 【发布时间】:2019-02-01 22:59:08 【问题描述】:在将 sql 查询转换为查询生成器时,我被卡住了,因为查询返回一个空集,而我知道原始 SQL 子句将返回约 200 条记录。
使用 toSql() 和控制台测试进行故障排除。我猜我有一个奇怪的“在哪里”或“在”扔东西,但我似乎无法确定它。我提前感谢你所有的眼睛。谢谢。
select [h].[commitment_desc],
[im].[item_id],
[im].[item_desc],
[d].[qty_committed],
[jpl].[price],
[d].[item_start_date] as [item_commitment_start_date],
[il_c].[qty_on_hand] as [CPP_oh],
[il_b].[qty_on_hand] as [Bloomington_oh]
from [item_commitment_hdr] as [h]
left join [item_commitment_detail] as [d]
on [h].[item_commitment_hdr_uid] = [d].[item_commitment_hdr_uid]
and [d].[item_start_date] < ?
and [d].[item_end_date] > ?
inner join [inv_mast] as [im]
on [d].[inv_mast_uid] = [im].[inv_mast_uid]
inner join [job_price_hdr] as [jph]
on [jph].[job_no] = ?
left join [job_price_line] as [jpl]
on [jph].[job_price_hdr_uid] = [jpl].[job_price_hdr_uid]
and CAST (im.inv_mast_uid as nvarchar(15)) = jpl.inv_mast_uid
and [jpl].[row_status_flag] = ?
left join [inv_loc] as [il_b]
on [d].[inv_mast_uid] = [il_b].[inv_mast_uid]
and [il_b].[location_id] = ?
and [il_b].[delete_flag] = ?
left join [inv_loc] as [il_c]
on [d].[inv_mast_uid] = [il_c].[inv_mast_uid]
and [il_c].[location_id] = ?
and [il_c].[delete_flag] = ?
where [h].[customer_id] = ?
and [h].[row_status_flag] = ?
and [h].[commitment_start_date] < ?
and [h].[commitment_end_date] > ?
现在是 php
$liability = DB::connection('p21')->table('item_commitment_hdr as h')
->select(
'h.commitment_desc',
'im.item_id',
'im.item_desc',
'd.qty_committed',
'jpl.price',
'd.item_start_date as item_commitment_start_date',
'il_c.qty_on_hand as CPP_oh',
'il_b.qty_on_hand as Bloomington_oh'
)->leftjoin('item_commitment_detail as d', function ($join) use ($now)
$join->on('h.item_commitment_hdr_uid', '=', 'd.item_commitment_hdr_uid')
->where('d.item_start_date', '<', $now)
->where('d.item_end_date', '>', $now);
)->join('inv_mast as im', 'd.inv_mast_uid', '=', 'im.inv_mast_uid')
->join('job_price_hdr as jph', function ($join) use ($job_no)
$join->where('jph.job_no', '=', "'" . $job_no[0] . "'");
)->leftjoin('job_price_line as jpl', function ($join)
$join->on('jph.job_price_hdr_uid', '=', 'jpl.job_price_hdr_uid')
->whereRaw('CAST (im.inv_mast_uid as nvarchar(15)) = jpl.inv_mast_uid')
->where('jpl.row_status_flag', '=', (int)704);//active flag
)->leftjoin('inv_loc as il_b', function ($join) use ($mainWarehouse)
$join->on('d.inv_mast_uid', '=', 'il_b.inv_mast_uid')
->where('il_b.location_id', '=', (int)$mainWarehouse)
->where('il_b.delete_flag', '=', "'N'");
)->leftjoin('inv_loc as il_c', function ($join) use ($consignmentWarehouse)
$join->on('d.inv_mast_uid', '=', 'il_c.inv_mast_uid')
->where('il_c.location_id', '=', (int)$consignmentWarehouse)
->where('il_c.delete_flag', '=', "'N'");
)->where('h.customer_id', '=', (int)$AccountGroup[0]->corp_address_id)
->where('h.row_status_flag', '=', (int)704)
->where('h.commitment_start_date', '<', $now)
->where('h.commitment_end_date', '>', $now)
->orderBy('im.item_id')
//->toSql();
->get();
【问题讨论】:
请添加所有变量。 【参考方案1】:我注意到的一件事是,您左连接表 item_commitment_detail,然后在 item_commitment_detail.inv_mast_uid 上内连接 inv_mast。
这有效地将(左)外部联接更改为内部联接。
如果 item_commitment_detail 必须是左连接,那么 inv_mast 也应该是左连接。
考虑到样式和可读性,您可以考虑先列出所有内连接,然后再列出外连接。这将立即显示出这种(不需要的)关系。
【讨论】:
【参考方案2】:好的,事实证明定价表(标题和行)不满意......为什么我不知道,但是,将两个连接与子查询一起移动似乎已经解决了这个问题。
$prices = DB::connection('p21')->table('job_price_line as jpl')
->select('item_id', 'uom', 'price')
->join('job_price_hdr as jph', function ($join) use ($job_no)
$join->on('jpl.job_price_hdr_uid', '=', 'jph.job_price_hdr_uid')
->whereRaw('jph.job_no = ' . $job_no[0]);
)->join('inv_mast as im', 'jpl.inv_mast_uid', '=', 'im.inv_mast_uid')
->whereRaw('jpl.row_status_flag = 704')
->toSql();
然后
->join(DB::raw("(" . $prices . ") as p"), function ($join)
$join->on('im.item_id', '=', 'p.item_id');
【讨论】:
以上是关于我雄辩的查询构建器实例返回空,而 sql 子句返回结果。会欣赏第二只眼睛的主要内容,如果未能解决你的问题,请参考以下文章
laravel 雄辩的查询构建器更新自定义时间戳字段而没有任何逻辑
Laravel 雄辩的 SQL 查询与 OR 和 AND 与 where 子句