jeudi 28 décembre 2017

Laravel 5.4 - DB Query Assistance

I am currently trying to write a query to display the value of 7 days worth of logs, which are automated into my table, for example the below data.

DB Structure

So what I am wanting is it to select the 7 newest entries in the posts (e.g. id 465-471) and then calculate them all together and spit out the number, e.g. 4353 for 7 latest enties of the above data.

The query I've written via SQL is as below:

SELECT SUM(posts) as poststw FROM (SELECT posts FROM `stats_log` ORDER BY dateline DESC LIMIT 7) tl

and this works successfully and manages to display via PhpMyAdmin the count correctly.

Results from query

I tried converting myself, and writing this in Laravel format as below, but it only ever displays 0 from the first line (to prevent page failing when query fails).

$posts_this_week = 0;
$posts_this_weeks = DB::select(DB::raw('SELECT SUM(posts) as poststw FROM (SELECT posts FROM `stats_log` ORDER BY dateline DESC LIMIT 7) tl'));

Can anyone assist me in writing the correct query which shows the correct number, as I'm having a mental blank right now!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2zIsCUm
via IFTTT

Aucun commentaire:

Enregistrer un commentaire