This question already has answers here:
Select last row in MySQL
(11 answers)
Closed 2 years ago.
I want to display only the latest record from usersId.
How do I create a query that would give me the latest ordersId from usersId?
The table looks like this:
this the query that I use but it displays all the ordersId:
SELECT * FROM `orders` WHERE usersId=?
You can use the LIMIT clause as follows:
SELECT * FROM `orders` WHERE usersId=?
order by orderdate desc limit 1;
OR you can use analytical function row_number as follows:
select * from
(SELECT t.*,
row_number() over (partition by usersId order by orderdate desc) as rn
FROM `orders` t WHERE usersId=?) t
where rn = 1
row_number solution is useful when you want the latest data for multiple usersid.
To get the latest one record, please
set order by OrdersID in descending order, and then
get only 1 record by using "limit 0, 1"
So please change the
SELECT * FROM `orders` WHERE usersId=?
to
SELECT * FROM `orders` WHERE usersId=? order by ordersId desc limit 0,1
You are binding $uid before setting it to the user_id from session you should assign first & then bind.
$uid=$_SESSION['userid'];
$stmt->bind_param("s",$uid);
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 months ago.
I have a table named Work_Items like this:
Assume there are lots of Names (i.e., E,F,G,H,I etc.,) and their respective Date and Produced Items in this table. It's a massive table, so I'd want to write an optimised query.
In this, I want to query the latest A,B,C,D records.
I was using the following query:
SELECT * FROM Work_Items WHERE Name IN ('A','B','C','D') ORDER BY Date DESC OFFSET 0 LIMIT 4
But the problem with this query is, since I'm ordering by Date, the latest 4 records I'm getting are:
I want to get this result:
Please help me in modifying the query. Thanks.
On MySQL 8+, we can use ROW_NUMBER:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Date DESC) rn
FROM Work_Items
WHERE Name IN ('A', 'B', 'C', 'D')
)
SELECT Name, Date, ProducedItems
FROM cte
WHERE rn = 1
ORDER BY Name;
You can use inner join as follows, its working on any mysql version:
select w.name, w.`date`, w.ProducedItems
from _Work_Items w
inner join (
select name, max(date) as `date`
from _Work_Items
group by name
) as s on s.name = w.name and s.`date` = w.`date` ;
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 5 months ago.
SQL query for extracting last entered number field(if we enter 11 in 3 rows take last row) from a table.Consider the following table
I want to show result like this
On MySQL 8+, we can use ROW_NUMBER here:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY NO ORDER BY created_date DESC) rn
FROM yourTable
)
SELECT NO, Letter
FROM cte
WHERE rn = 1;
The above would return the latest row for every NO group. If instead you want the earliest row, then change the sort order used by ROW_NUMBER.
Another option for you
SELECT a.NO,a.Letter FROM yourtable a
JOIN
(SELECT max(date) AS date,NO FROM yourtable GROUP BY NO) b ON a.NO=b.NO AND a.DATE=b.DATE
ORDER BY a.NO
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 1 year ago.
i have a table named messages like this :
I want query for where reciever_id equal 1 and group by sender_id and get latest record.
I USED QUERY :
SELECT `t`.* FROM(
SELECT * FROM messages
WHERE reciever_id = 1
ORDER BY created_at DESC
) `t`
GROUP BY `sender_id`
ORDER BY `id`
AND ALSO :
SELECT message, MAX(created_at)
FROM messages
WHERE reciever_id = 1
GROUP BY sender_id
ORDER BY created_at
Date's column created_at in picture exactly are the latest and id's also ordered and are latest also.
I'm done this after releasing question by below query, but i think this can cost more than others... there are another way to do this with low cost?!
Can somebody say formula how times multiple joins below query has cost?
SELECT id,sender_id,reciever_id,seen,message,created_at
FROM messages
WHERE id IN (
SELECT MAX(id)
FROM messages
WHERE reciever_id = 1
GROUP BY sender_id
ORDER BY id desc
) ORDER BY created_at DESC
I have a mysql query which will return all the details from table along with i need max_row count i.e total no of rows in a table using COUNT(*) in a single select query without using cross join.
Note: MySQL version is earlier version of 8
Query :
SELECT * FROM tablename ORDER BY column name DESC LIMIT 0,10;
The total count of a table is simple, when you want to add it to every row.
SELECT
*
,(SELECT COUNT(*) FROM tablename ) count1
FROM tablename
ORDER BY column name
DESC LIMIT 0,10;,
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I have a history table which I would like to use to find the latest user in which updated specific items. Here is the query I have so far:
SELECT *
FROM `history`
WHERE `pKey`
IN ( 13309, 13311, 13951, 14244, 1500, 15558, 15691, 15938, 9769 )
ORDER BY `history`.`time` DESC
LIMIT 0 , 30
This returns multiple history results for each pkey. Is there a way to limit the results to only the latest (based on time) entry from the specific pkey?
So for example:
Right now pkey 13309 has multiple results returned. The query should only return the latest result for it. Same goes for 13311... etc.
This should do:
SELECT h.*
FROM `history` as h
INNER JOIN (SELECT `pkey`, MAX(`time`) as MaxTime
FROM `history`
WHERE `pkey` IN (13309, 13311, 13951, 14244, 1500,
15558, 15691, 15938, 9769)
GROUP BY `pkey`) as t
ON h.`pkey` = t.`pkey`
AND h.`time` = t.`MaxTime`
this should work. Just grouping all the rows that have the same pkey. I think this will work. Comment with a feedback.
Select * from (
SELECT *
FROM `history`
WHERE `pKey`
IN ( 13309, 13311, 13951, 14244, 1500, 15558, 15691, 15938, 9769 )
ORDER BY `history`.`time` DESC) as t1 group by pKey