Get data from database using date groups in laravel [duplicate] - php

This question already has answers here:
Laravel Eloquent get results grouped by days
(17 answers)
Closed 2 years ago.
I am using laravel and I am trying to get data from database grouped by the column "created_at".
The data into created_at column is being stored is in the below format,
2020-07-23 07:03:59
My question is , how can we fetch the data using group for dates only.
For example , All the records should be grouped into one array/object with same dates.
I tried executing the following query,
$transactions= MyTransaction::where('id','=',$user->id)->orderBy('created_at')->get()->groupBy(function($item) {
return $item->created_at->format('Y-m-d');
});
but it does not return what i am expecting, it considers the seconds and hours into the consideration too.
Please guide me solving this query.

$column = \DB::raw("CONCAT(YEAR(`ride_date`),'-',MONTH(`ride_date`),'-',DAY(`ride_date`)) AS `group_column`");
$transactions= MyTransaction::select('*',$column)->where('id','=',$user->id)->orderBy('created_at')->groupBy('group_column')->get();

Related

how to count value in laravel 8 eloquent? [duplicate]

This question already has answers here:
How to count value in Laravel Eloquent?
(4 answers)
Closed 1 year ago.
I want to count member that has been time in each day but dont have a time out yet. In short i want to count member in particular date that doesnt have time out. This is because i want to count all the member inside the compound for safety guidelines. For example G-51405 been time in but no time out in the table, G-51405 will be counted because he/she dont have a time out yet and so on..
$memberCode = 'G-51405';
$membersCount = Member::where('status','in')->where('member_code', $memberCode)->count();

How to show last data with created date? [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Retrieving the last record in each group - MySQL
(33 answers)
Closed 3 years ago.
I have a problem to show one of duplicate data with last created date. This is is my query :
$sqlpur ="SELECT DISTINCT pn, created_date, id_purchase FROM pur_supp ORDER BY pn, created_date DESC";
This is the result.
This is a data from database.
But, I want to show just one of duplicate data. an example just to show 02N64073 with last created Date.

How to return the latest added rows from MySql database. [duplicate]

This question already has answers here:
How can I return 10 of the most recent results in sql?
(4 answers)
Closed 9 years ago.
How can i ask a question to a mysql-database (via PDO) to return the 3 latest added rows? The id column is key and auto incremented. That means that highest id means latest added. Don't take into account the fact that rows can have been deleted and such.
Could i somehow use * and LIMIT 3 and start from the bottom some way - or something?
Should be fairly easy but i am kind of stuck.
Order by ID desc limit 3
gives you the three rows with the highest ID. Those are not necessarily the latest three added rows. But they are the latest three added rows still existing in the table.

how to extend the maximum number of expression in a list of oracle? [duplicate]

This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I am using an IN clause, whereby, it needs to search the id IN these set of IDs that are not in order, meaning, it came from a different criteria., how to solve this ?
SELECT DISTINCT ID FROM "projeck"."mytable" "t" WHERE staffID IN (75953,196262,196387,133585,195639,196702,195790,195820,192903,145383,179603,175896,176554,43545,154843,183798,195767,195715,etc..etc.. etc..)
and i am getting this oracle error
General error: 1795 OCIStmtExecute: ORA-01795: maximum number of expressions in a list is 1000
My first choice would be to reference the function that generates these values directly.
If the values were being used for multiple queries and were expensive to calculate then I'd think about loading them into a global temporary table and joining to it.

MySQL replace field entry with substring [duplicate]

This question already has answers here:
Converting a date in MySQL from string field
(4 answers)
Closed 9 years ago.
I currently have dates stored in my database (in string format) currently as mm/dd/yyyy.
I need to replace all entries as yyyy/mm/dd for sorting purposes.
To clarify, I am a PHP developer but my raw MySql isnt as strong. I was thinking of doing this by using substrings to re-arrange the date. (I tried in PHP first but it take way too long to process this much data, about a half million rows).
Am i going about this correctly? if so, how do I "loop through" the data, assigning my substr variables for the current iteration?
Note: I did try str_to_date here and it ended up nullifying half of the fields. Im not entirely sure why.
Your help is greatly appreciated.
this is an insert statment
INSERT INTO yourtable (datefield) VALUES (str_to_date(date, '%Y/%m/%d'));
and this an update
UPDATE Table SET date=STR_TO_DATE('date','%Y/%m/%d')
try this
UPDATE Table SET date = DATE_FORMAT('date', %Y/%m/$d);
I believe this will work if your column is in a consistent mm/dd/yyyy format
UPDATE mytable
SET date = CONCAT(right(date,4), '/', left(date,5))

Categories