select(array( 'users.*', DB::raw("CONCAT (firstna-6ren">
gpt4 book ai didi

mysql - 如何修复此查询连接查询?

转载 作者:行者123 更新时间:2023-12-02 18:39:11 25 4
gpt4 key购买 nike

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

$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();

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

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

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

关于mysql - 如何修复此查询连接查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68296797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com