如何修复此查询concat查询?

这是我想要执行的,但它不起作用:

$users = DB::table("users")
->select(array(
  'users.*',
  DB::raw("CONCAT (firstname, ' ', lastname) as fullName "),
))
->where("fullName", "like", $query)
->get();

正如预期的那样,我收到此错误:

Column not found: 1054 Unknown column 'fullName' in 'where clause'

有没有办法让 where 子句知道 fullName?我知道我可以这样做:

$users = DB::table("users")
->select(array(
  'users.*',
  DB::raw("CONCAT (firstname, ' ', lastname) as fullName "),
))
->where(DB::raw("CONCAT (firstname, ' ', lastname) like ".$query))
->get();

但是如果我这样做,那么我需要清理 $query,如果准备好的语句像在第一个示例中那样为我处理它,我更喜欢它。

知道如何解决这个问题吗?

回答

使用having()代替where()

$users = DB::table("users")
->select(array(
  'users.*',
  DB::raw("CONCAT (firstname, ' ', lastname) as fullName "),
))
->having("fullName", "like", $query)
->get();

并更改检查 DB 必须在严格模式下运行的配置设置:

in /config/database.php: 严格执行false

'mysql' => [
        ----
       ----
       'strict' => true,   // <--- Change this to false
       ----
 ],

  • 请参阅此链接:/sf/ask/2947308871/

以上是如何修复此查询concat查询?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>