Laravel 中的 DB Raw 查询错误
Posted
技术标签:
【中文标题】Laravel 中的 DB Raw 查询错误【英文标题】:DB Raw query error in Laravel 【发布时间】:2018-04-15 20:39:55 【问题描述】:这是我的 sql.. 当我将它运行到 phpmyadmin 时,它运行正常。但是当我要以 DB:raw in laravel5.5 运行时,它会显示错误。这里,Auckland 不是列名。
$result = DB::select("select DISTINCT(SELECT count(id) FROM Commercial_lease where LENGTH(CONCAT(region,city,"Auckland")) = LENGTH(location) ) as Listing_without_address ,(SELECT count(id) FROM Commercial_lease where LENGTH(CONCAT(region,city,"Auckland"))
【问题讨论】:
【参考方案1】:您正在混合引用。查询被双引号覆盖,对于Auckland
,您试图再次将它们放在双引号中,这会发出错误。
要解决此问题,您可以转义字符串,例如 \"Auckland\"
或使用单引号 'Auckland'
$result = DB::select("select DISTINCT
(SELECT count(id) FROM commercial_lease where LENGTH(CONCAT(region,city,'Auckland')) = LENGTH(location) ) as listing_without_address ,
(SELECT count(id) FROM commercial_lease where LENGTH(CONCAT(region,city,\"Auckland\")) < LENGTH(location)) as listing_with_address ,
(SELECT count(id) FROM commercial_lease WHERE first_agent_name = 'None' and second_agent_name='None') as private_listing from commercial_lease");
【讨论】:
以上是关于Laravel 中的 DB Raw 查询错误的主要内容,如果未能解决你的问题,请参考以下文章