带有两个查询的Union返回“调用未定义的方法stdClass:: Union()”错误

时间:2022-09-20 15:04:07

I'm currently trying to union two queries but unfortunately they return an error.

我目前正在尝试联合两个查询,但不幸的是它们返回了一个错误。

$bitfinex = DB::table('bitfinex')->select('price')->latest()->first();
$bitstamp = DB::table('bitstamp')->select('price')->latest()->first()->union($bitfinex);

Returns this error:

返回这个错误:

(1/1) FatalThrowableError
Call to undefined method stdClass::union()

I'd appreciate any help, thanks in advance!

非常感谢您的帮助,提前谢谢!

1 个解决方案

#1


2  

first() is fetching the first result from your query. At that point, the query is already submitted to the database server and can't be unioned.

首先()是从查询中获取第一个结果。此时,查询已经提交到数据库服务器,无法进行联合。

If you want only one result from each select, use the limit() method, which corresponds to SQL limit.

如果您希望每个select中只有一个结果,请使用limit()方法,它对应于SQL limit。

$bitfinex = DB::table('bitfinex')->select('price')->latest()->limit(1);
$bitstamp = DB::table('bitstamp')->select('price')->latest()->limit(1);

$results = $bitfinex->union($bitstamp)->get();

get() will fetch a collection of the results, as opposed to first() which will only fetch the first result (row).

get()将获取结果的集合,而first()只获取第一个结果(行)。

#1


2  

first() is fetching the first result from your query. At that point, the query is already submitted to the database server and can't be unioned.

首先()是从查询中获取第一个结果。此时,查询已经提交到数据库服务器,无法进行联合。

If you want only one result from each select, use the limit() method, which corresponds to SQL limit.

如果您希望每个select中只有一个结果,请使用limit()方法,它对应于SQL limit。

$bitfinex = DB::table('bitfinex')->select('price')->latest()->limit(1);
$bitstamp = DB::table('bitstamp')->select('price')->latest()->limit(1);

$results = $bitfinex->union($bitstamp)->get();

get() will fetch a collection of the results, as opposed to first() which will only fetch the first result (row).

get()将获取结果的集合,而first()只获取第一个结果(行)。