How can I use 'AS' in my count query?

user9888683

How can I use AS in my count query?

Actually i want to result like { "count":"number" } for json result. I dont know what should i call this thing?

public function firstHourTrades(){

    $user_id = Auth::user()->id;
    $data = DB::table('finaltrade')
        ->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')

        ->where('finaltrade.user_id', $user_id)
        ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time'))
        ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '1:00:00')"))
        ->count();

    return response()->json($data);
}
Tim Biegeleisen

You could also do a raw select and manually assign the alias:

$data = DB::table('finaltrade')
    ->select(DB::raw('count(*) as count'))
    ->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
    ->where('finaltrade.user_id', $user_id)
    ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time'))
    ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '1:00:00')"))
    ->get();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use count in query to count 0 if data is empty

How can I use my SQL query in the cursor query?

How can I use the MySQL COUNT() statement on a generated query?

How can I use my model properties in a Linq query?

How can I use an input from another table in my query?

MongoDB - How do I extract count from a variable and use it in my query for mongodb

How can I animate a MySQL query count?

How can I count occurrences of different types from my table using a raw query?

How can i count this variables in my DB?

How can i count results in my sql?

how can i pass this query to my controller in codeigniter and to use this in my foreach in view

How can I include my query in my query results?

How can I use enumerate to count backwards?

How can i use distinct with count in Linq?

How can I use COUNT() result in RANK

How I can optimize my LINQ query?

How can i Optimize my sql query

How can I use select count (distinct x) in order to count two values in the same table and get the the two distinct values in my output?

How can I rewrite sql query with having count to sequelize query?

How can I add a field that count the number of the records retrieved by my query? Why am I obtaining this error message?

How do i count unique (distinct) values in my MySQL query

How do I calculate a distinct running country count for my query?

Google Stackdriver - how can I use my Kubernetes YAML labels for Stackdriver Log Query?

How can I use, in Visual Studio 2017, the "C# Interactive" window to query a source in my "Data Connections"

How can I use a subquery on my query results and then ORDER BY a calculated result for each row?

How can i change this LINQ query to use my List<int> instead of the hard coded number?

How can I use a variable in a query selector?

How can I use NOT BETWEEN in a query in Phalcon?

How can I use in query in mongodb?