![](/img/trans.png)
[英]How to avoid “Ambiguous field in query” without adding Table Name or Table Alias in where clause
[英]How run query in laravel where clause without table column name?
如何在 Laravel 中进行此查询,我是 laravel 中的新手
Product_Type::addSelect([
'p_name' => Product::select('p_name')->whereColumn('products.id','product_types.products_id'),
'product_types.pt_name',
'sale_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','sale')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
'return_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','return')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
])->where('product_types.status',1)->get()
它的工作,但我只想得到sale_qty > 0所以当在 where 子句中设置 sale_qty 查询然后得到错误我想更正这个查询
Product_Type::addSelect([
'p_name' => Product::select('p_name')->whereColumn('products.id','product_types.products_id'),
'product_types.pt_name',
'sale_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','sale')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
'return_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','return')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
])->where('product_types.status',1)
->whereRaw(Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','sale')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))) > 0)->get()
在 MySQL 查询中,根据我的要求显示正确的结果
SELECT
(SELECT p_name FROM products WHERE id=product_types.products_id) AS p_name,
pt_name,
(SELECT SUM(stock_value)*-1 FROM stocks WHERE product_types_id=product_types.id AND stock_value < 0 AND stock_type='sale' AND stock_date BETWEEN '2022-07-01' AND '2022-08-31' AND id IN (SELECT stocks_id FROM bills WHERE status=1 AND customers_id IN (SELECT id FROM customers WHERE sale_mans_id=1))) AS sale_qty,
(SELECT SUM(stock_value)*-1 FROM stocks WHERE product_types_id=product_types.id AND stock_value < 0 AND stock_type='return' AND stock_date BETWEEN '2022-07-01' AND '2022-08-31' AND id IN (SELECT stocks_id FROM bills WHERE status=1 AND customers_id IN (SELECT id FROM customers WHERE sale_mans_id=1))) AS return_qty
FROM product_types
WHERE
status =1 AND
(SELECT SUM(stock_value)*-1 FROM stocks WHERE product_types_id=product_types.id AND stock_value < 0 AND stock_type='sale' AND stock_date BETWEEN '2022-07-01' AND '2022-08-31' AND id IN (SELECT stocks_id FROM bills WHERE status=1 AND customers_id IN (SELECT id FROM customers WHERE sale_mans_id=1))) > 0;
因为 where 子句不允许没有列名所以没有列名如何管理
您可以使用->whereRaw
并根据需要定义条件。 它将作为字符串插入到完整查询中,无需任何修改。 这样你就可以完全控制它。
查看相关文档: https://laravel.com/docs/9.x/queries#whereraw-orwhereraw
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.