join("recommends", "topics.id", "=", "recommends.cou-6ren">
gpt4 book ai didi

php - Laravel 4 union 生成错误的 SQL

转载 作者:行者123 更新时间:2023-11-30 01:15:45 25 4
gpt4 key购买 nike

这是代码:

    $f = DB::table("topics")
->join("recommends", "topics.id", "=", "recommends.courseid")
->where("recommends.re_type", "=", $re_type)
->where("recommends.re_location", "=", $re_location)
->orderBy("recommends.weigh", "desc");

$s = DB::table("topics")
->orderBy("topics.create_time", "desc");

$f->union($s)->get();

我在关键字 union 周围得到了错误的 SQL:

    select * from `topics` inner join `recommends` 
on `topics`.`id` = `recommends`.`courseid`
where `recommends`.`re_type` = ?
and `recommends`.`re_location` = ?
order by `recommends`.`weigh` desc
union //here!!!!!
select * from `topics` order by `topics`.`create_time` desc

错误信息:

SQLSTATE[HY000]: General error: 1221
Incorrect usage of UNION and ORDER BY (SQL: ...)
(Bindings: array ( 0 => 3, 1 => 7, ))

问题是什么?

最佳答案

MySQL UNION 期望所有语句中具有相同的列。由于您要连接 $f 中的另一个表,因此两个语句之间的列不匹配。

参见MySql SELECT union for different columns?

在这种情况下,直接使用 PDO 对象可能不会那么麻烦。

$pdo = DB::connection()->getPdo();

关于php - Laravel 4 union 生成错误的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19069120/

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