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();
This question already has answers here:
How to retrieve the last 4 characters from a mysql database field?
(4 answers)
Getting last 5 char of string with mysql query
(6 answers)
Query to select strings end with certain character
(6 answers)
Closed 5 years ago.
this is my data in the database and i'm using mysql
database image
How do I retreive number "4" from the data using sql command?
USE reverse and left
set #myfield = 1234;
select left(reverse(#myfield),1) as result;
result
4
demo here
http://rextester.com/LTBE34983
This question already has answers here:
Returning query results in predefined order
(11 answers)
Closed 6 years ago.
I have a IN clause query in which I pass this ID'S (10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
My query is,
SELECTpost_jobs.idFROMpost_jobsWHEREidIN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
It execute correctly and returns result first id likes 1 then 4 then 7 and so on...that is ascending order
My Need is to get first result of id 10 then 19 then 23 as the order of ID that I passed is there any way to get such type of result in output.
Try this
SELECT post_jobs.id FROM post_jobs WHERE id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIELD(id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
In Codeigniter
$this->db->_protect_identifiers = FALSE;
$this->db->select('id');
$this->db->where_in('id',array(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29));
$this->db->order_by('FIELD(id, 10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)');
$this->db->get('post_jobs');
If you are using codeigniter 3 then write or remove the line of protect_identifiers
$this->db->protect_identifiers(False);
Try this one
SELECT post_jobs.id FROM post_jobs WHERE post_jobs.id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIND_IN_SET(post_jobs.id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
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.