How to get last 3 records - mysql

I am adding new record for every transaction
For Example:
microfinance,
Today customer paid 300, then new record is inserted
tomorrow 500 again new record is inserted)
then how can i get last 3 records based on customer id.
table
ID Password Amount
1 a 200
2 s 500
1 a 100
3 b 200
1 a 300
1 a 300
there is no primary key
i want last inserted 3 records for id=1

I believe you need something like this:
SELECT *
FROM Table
ORDER BY id DESC
LIMIT 0,3

SELECT Id
,Password
,Amount
,#curRow := #curRow + 1 AS row_number
FROM sample JOIN (SELECT #curRow := 0) r
where id=1
order by Row_number
desc limit 3;
Working Fiddle

Related

mysql last 5 records in count status

booking table
id status
1 booked
1 booked
3 cancelled
2 cancelled
2 booked
1 cancelled
1 cancelled
1 booked
select id, count(status) as tot_cancel
from tbl_booking
where id=1 and status='cancelled';
result will be
id tot_cancel
1 2
BUT I NEED last 5 records of user id = 1 and cancelled count not total table record cancelled
I understand that you want the count of cancelled records over the last 5 records of user 1. It is possibe, but you need a column that defines the ordering of the rows, so it is unambiguous which records are last.
Assuming that such column exists in your table and is called ordering_id, you can do:
select sum(status = 'cancelled') no_cancelled
from (select status from mytable where id = 1 order by ordering_id desc limit 5) t
You can do conditional aggregation with limit clause :
select 1 as id, sum(status = 'cancelled')
from table t
where id = 1
order by ? -- use ordering column instead ?
limit 5;
You can use correlated subquery for all ids :
select t.id, sum(t.status = 'cancelled')
from table t
where t.? in (select t1.?
from table t1
where t1.id = t.id
order by t1.?
limit 5
)
group by t.id;

How can I get the second higher number in a query

Example, Lets suppose I have a table like next one:
id | start | userId
1 3 1
2 2 2
3 5 1
Now, I want to get all the row information for the second higher value of the column start related to some userId. So, if userId equals to 1 I'm expecting to get the next result:
(id, start, userId) = (1,3,1)
I have tried this query:
SELECT id, max(start) FROM table_user WHERE userId = 1;
But this gives me the higher number.
You can do this easy using ordering by column start and the features of LIMIT, like this:
SELECT
*
FROM
table_user
WHERE
userId = 1
ORDER BY
`start` DESC LIMIT 1, 1
You can check this online also: DB-Fiddle

How to get last inserted 3 records with condition mysql

am adding new record for every transaction( for ex; microfinance, today customer paid 300, then new record is inserted,tomorrow 500 again new record is inserted) thyen how can i get last 3 records based on customer id.
SELECT Id
,Password
,Amount
,#curRow := #curRow + 1 AS row_number
FROM sample JOIN (SELECT #curRow := 0) r
where id=1
order by Row_number
desc limit 3;
Working Fiddle
you can use order by desc customer id and get last 3 record with limit 0,3:
select * from tbname
order by customerid desc
limit 0,3
SELECT * FROM table ORDER BY id DESC LIMIT 3;
First, order your select using ORDER BY and then use LIMIT to get first records.
http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
http://dev.mysql.com/doc/refman/5.0/en/select.html

How to select only the last value in a query in MySQL?

How can I select on MySQL the last value of this result:
This is a result from query:
SELECT * from transaction WHERE transaction_id = 2
I just need the last value 3 300 2
bank_id amount transaction_id
1 800 2
3 50 2
3 300 2
If bank_id is not unique and you want to pick the record of highest amount first, you can try this one:
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY bank_id DESC
, amount DESC
LIMIT 1
See this SQLFiddle
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY bank_id desc
LIMIT 1
Here is SQLFiddel Demo
This Demo, selects Last Entry in Transaction table with your filter of Transaction_ID = 2.
Below is the Query which you can try.
select *,#curRow := #curRow + 1 AS row_number
from Temp
Join (SELECT #curRow := 0) r
where Transaction_id = 2
order by row_number desc
limit 1
try out this..
SELECT *
FROM transaction
WHERE transaction_id = 2
ORDER BY transaction_id DESC
LIMIT 1

getting rank of a row in a rowset

I have the following rows :
id name score
1 Bill 0
2 Kim 8
3 Michael 30
4 Doug 22
5 Mellisa 1
When I do SELECT * FROM table ORDER BY score DESC, I get
id name score
4 Doug 22
3 Michael 30
2 Kim 8
5 Mellisa 1
1 Bill 0
Now I want to get only one row, so I'll add a where clause, but problem is the ranking.
I'm producing pseudo ranks in my php code, i.e. the first row in the rowset is assigned rank 1, second is assigned rank 2 and so on...
So when I do SELECT * FROM table WHERE id = 5, apparently all the rank information is lost. How to get over this problem? Is there any way I could get the position of the row in rowset before adding the where clause?
Wrap the query in a subquery so that the rank will be preserved. Here's an example,
-- the example produces rank based on highest score.
SELECT *
FROM
(
SELECT #rank := ifnull(#rank, 0) + 1 Rank,
Id, name, Score
FROM tableName a
ORDER BY Score DESC
) x
WHERE ID = 5
SQLFiddle Demo
SELECT id, name, score, FIND_IN_SET( score, (
SELECT GROUP_CONCAT( score
ORDER BY score DESC )
FROM ScoreDetail)
) AS rank
FROM ScoreDetail
Where id=5
This will solve your problem....