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

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.

Related

How to update table by date with two columns in another table which has specific range of date in a year? [duplicate]

This question already has answers here:
How to update a row based a joined table in MariaDB?
(2 answers)
MariaDB/MySQL UPDATE statement with multiple joins including a ranged join
(1 answer)
MariaDB update error inner join and select
(2 answers)
UPDATE with JOIN not SELECTing expected value
(2 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I have two tables, the first is MainDB where I want to get the service fee in columns MainDB.COL5 equal to ServiceFEE.COL_5, the primary key is the date in MainDB.COL_4 which falls in range between two ServiceFEE.COL_3 and ServiceFEE.COL_3.
for the service fee period may consider by start date and end date
some countries may have many different of service fees in a year
UPDATE MainDB SET MainDB.COL_5 = ServiceFEE.COL_5 WHERE MainDB.COL_4 = BETWEEN ServiceFEE.COL_3 and ServiceFEE.COL_4

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

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();

how to retreive last number in the data [duplicate]

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

rearrangement of IN clause output [duplicate]

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);

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.

Categories