Laravel:数学在WHERE子句
问题描述:
在SQL这是相当容易做到像下面这样:Laravel:数学在WHERE子句
SELECT * FROM properties WHERE (price/acres) = 3000;
这将选择每个属性里的“价格每亩”是3000
我希望做这在Laravel。我试过如下:
Property::where('price/acres', 3000)->get();
但是这个包裹在反引号的列标题,创建下列SQL:与错误
select * from `properties` where `price/acres` = 3000
这失败(显然):SQLSTATE[42S22]: Column not found: 1054 Unknown column 'price/acres' in 'where clause'
答
你是对的你不能归因于Laravel中的那些语句。你总是有一个选择是提交使用DB门面原始SQL语句 - https://laravel.com/docs/5.3/database#running-queries
例如:
$price_per_acre = DB::select('SELECT * FROM properties WHERE (price/acres) = 3000');
没关系啊,这是现在可能是一个更好的办法,我想想吧! – DrewT
没有我想要的那么优雅,因为我现在不得不清理并逃避用户输入,但它起作用 – stevendesu
如果您需要传递一个数字作为变量,请将3000更改为?,然后传递它。 'property :: whereRaw('(price/acres)=?',[$ yourvariable]) - > get();'这不能用列名完成。 – aynber