query: 取 3 条记录,哪条记录值大
query: fetch 3 records which has higher value
我想获取 3 个 symbol_id
具有更高的 buy_rate
值
finaltrades
table 结构:
id user_id exchange_id market_id symbol_id buy_datetime sell_datetime buy_rate sell_rate quantities
---- --------- ------------- ----------- ----------- --------------------- --------------------- ---------- ----------- ------------
1 1 1 1 96 2018-05-25 18:13:26 0000-00-00 00:00:00 2205 0 100
2 1 1 1 96 0000-00-00 00:00:00 2018-05-25 18:13:59 500 6680 100
3 4 1 1 23 2018-05-25 18:16:27 0000-00-00 00:00:00 120 0 10
4 1 1 1 96 2018-05-25 18:13:59 0000-00-00 00:00:00 50351 0 30
5 1 1 1 15 0000-00-00 00:00:00 2018-05-25 18:34:46 750 100 150
6 4 1 1 573 2018-05-26 09:29:17 2018-05-27 03:10:09 107 10 10
7 1 1 1 15 2018-05-11 09:30:54 2018-05-25 18:34:56 40 100 40
这是我到目前为止的想法:
public function HigherValue(){
$higher_value = DB::table('finaltrade')
->select 'buy_rate'> (select 'buy_rate')
->get();
return response()->json($higher_value);
}
public function HigherValue()
{
$higher_value = DB::table('finaltrade')
->select('symbol_id')
->orderByDesc('buy_rate')
->limit(3)
->get();
return response()->json($higher_value);
}
试试这个
试试这个
public function HigherValue()
{
$higher_value = DB::table('finaltrade')
->select('symbol_id')
->orderBy('buy_rate', 'DESC')
->limit(3)
->get();
return response()->json($higher_value);
}
如果您想要三个不同的 symbol_id
具有最高的购买率,那么您可以尝试以下操作:
public function HigherValue() {
$higher_value = DB::table('finaltrade')
->select('symbol_id')
->groupBy('symbol_id')
->orderByRaw('MAX(buy_rate) DESC')
->limit(3)
->get();
return response()->json($higher_value);
}
这将对应于以下原始 MySQL 查询:
SELECT symbol_id
FROM finaltrade
GROUP BY symbol_id
ORDER BY MAX(buy_rate) DESC
LIMIT 3;
您可以使用 DISTINCT
来获得按 buy_rate
排序的不同 symbol_ids
SELECT DISTINCT symbol_id
FROM finaltrade
ORDER BY buy_rate DESC
LIMIT 3;
DB::table('finaltrade')
->orderBy('buy_rate', 'desc')
->distinct()
->limit(3)
->get();
我想获取 3 个 symbol_id
具有更高的 buy_rate
值
finaltrades
table 结构:
id user_id exchange_id market_id symbol_id buy_datetime sell_datetime buy_rate sell_rate quantities
---- --------- ------------- ----------- ----------- --------------------- --------------------- ---------- ----------- ------------
1 1 1 1 96 2018-05-25 18:13:26 0000-00-00 00:00:00 2205 0 100
2 1 1 1 96 0000-00-00 00:00:00 2018-05-25 18:13:59 500 6680 100
3 4 1 1 23 2018-05-25 18:16:27 0000-00-00 00:00:00 120 0 10
4 1 1 1 96 2018-05-25 18:13:59 0000-00-00 00:00:00 50351 0 30
5 1 1 1 15 0000-00-00 00:00:00 2018-05-25 18:34:46 750 100 150
6 4 1 1 573 2018-05-26 09:29:17 2018-05-27 03:10:09 107 10 10
7 1 1 1 15 2018-05-11 09:30:54 2018-05-25 18:34:56 40 100 40
这是我到目前为止的想法:
public function HigherValue(){
$higher_value = DB::table('finaltrade')
->select 'buy_rate'> (select 'buy_rate')
->get();
return response()->json($higher_value);
}
public function HigherValue()
{
$higher_value = DB::table('finaltrade')
->select('symbol_id')
->orderByDesc('buy_rate')
->limit(3)
->get();
return response()->json($higher_value);
}
试试这个
试试这个
public function HigherValue()
{
$higher_value = DB::table('finaltrade')
->select('symbol_id')
->orderBy('buy_rate', 'DESC')
->limit(3)
->get();
return response()->json($higher_value);
}
如果您想要三个不同的 symbol_id
具有最高的购买率,那么您可以尝试以下操作:
public function HigherValue() {
$higher_value = DB::table('finaltrade')
->select('symbol_id')
->groupBy('symbol_id')
->orderByRaw('MAX(buy_rate) DESC')
->limit(3)
->get();
return response()->json($higher_value);
}
这将对应于以下原始 MySQL 查询:
SELECT symbol_id
FROM finaltrade
GROUP BY symbol_id
ORDER BY MAX(buy_rate) DESC
LIMIT 3;
您可以使用 DISTINCT
来获得按 buy_rate
SELECT DISTINCT symbol_id
FROM finaltrade
ORDER BY buy_rate DESC
LIMIT 3;
DB::table('finaltrade')
->orderBy('buy_rate', 'desc')
->distinct()
->limit(3)
->get();