laravel - 交叉连接不返回所有列
laravel - cross join not returning all columns
╔═══╦══════════════╦═════════════╗
║ ║id ║name ║
╠═══╬══════════════╬═════════════╣
║ ║ 1 ║a1 ║
║ ║ 2 ║b1 ║
║ ║ 3 ║b2 ║
║ ║ 4 ║c1 ║
║ ║ 5 ║c2 ║
╚═══╩══════════════╩═════════════╝
这是我在 mysql
中的 table
正在 mysql 中执行此查询:
select * from (select * from courses where name like 'a%') as t1 cross join (select * from courses where name like 'b%') as t2 cross join (select * from courses where name like 'c%') as t3
returns这个:
然而,当我尝试 运行 在 larael 上显示结果时,我得到了一些不同的结果。
这就是我在 laravel 中的执行方式:
$posts = DB::select(DB::raw("select * from (select * from courses where name like 'a%') as t1 cross join (select * from courses where name like 'b%') as t2 cross join (select * from courses where name like 'c%') as t3"));
这就是 $posts
returns :
[{"id":4,"name":"c1"},{"id":4,"name":"c1"},{"id":5,"name":"c2"},{"id":5,"name":"c2"}]
我想它只返回查询的最后两列,如果我们将它与 mysql 上返回的原始结果进行比较。
知道如何得到完整的结果吗?
t3
覆盖 t1
和 t2
的值。您必须使用别名:
select t1.id as t1_id, t1.name as t1_name,
t2.id as t2_id, t2.name as t2_name,
t3.id as t3_id, t3.name as t3_name
您还应该使用 query builder 而不是原始 SQL。
╔═══╦══════════════╦═════════════╗
║ ║id ║name ║
╠═══╬══════════════╬═════════════╣
║ ║ 1 ║a1 ║
║ ║ 2 ║b1 ║
║ ║ 3 ║b2 ║
║ ║ 4 ║c1 ║
║ ║ 5 ║c2 ║
╚═══╩══════════════╩═════════════╝
这是我在 mysql
中的 table正在 mysql 中执行此查询:
select * from (select * from courses where name like 'a%') as t1 cross join (select * from courses where name like 'b%') as t2 cross join (select * from courses where name like 'c%') as t3
returns这个:
然而,当我尝试 运行 在 larael 上显示结果时,我得到了一些不同的结果。
这就是我在 laravel 中的执行方式:
$posts = DB::select(DB::raw("select * from (select * from courses where name like 'a%') as t1 cross join (select * from courses where name like 'b%') as t2 cross join (select * from courses where name like 'c%') as t3"));
这就是 $posts
returns :
[{"id":4,"name":"c1"},{"id":4,"name":"c1"},{"id":5,"name":"c2"},{"id":5,"name":"c2"}]
我想它只返回查询的最后两列,如果我们将它与 mysql 上返回的原始结果进行比较。
知道如何得到完整的结果吗?
t3
覆盖 t1
和 t2
的值。您必须使用别名:
select t1.id as t1_id, t1.name as t1_name,
t2.id as t2_id, t2.name as t2_name,
t3.id as t3_id, t3.name as t3_name
您还应该使用 query builder 而不是原始 SQL。