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?
this the query that I use but it displays all the ordersId:
SELECT * FROM `orders` WHERE usersId=?
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?
this the query that I use but it displays all the ordersId:
SELECT * FROM `orders` WHERE usersId=?
To get the latest one record, please
So please change the
SELECT * FROM `orders` WHERE usersId=?
to
SELECT * FROM `orders` WHERE usersId=? order by ordersId desc limit 0,1
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
.
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);