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
Related
My data looks like,
Table - usr_weight
user_id
weight
log_time
1.
10
2021-11-30 10:29:03
1.
12
2021-11-30 12:29:03
1.
11
2021-11-30 14:29:03
1.
18
2021-12-01 08:29:03
1.
12
2021-12-15 13:29:03
1.
14
2021-12-15 17:29:03
Here, I have duplicates for each date with different time. So, group date and return the record with max time for each date.
Query
select weight, log_time from usr_weight where user_id = 1 group by DATE(log_time)
Here, I get 1 record for each date, but the row is not by max(log_time).
Using ROW_NUMBER we can try:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DATE(log_time)
ORDER BY log_time DESC) rn
FROM usr_weight
WHERE user_id = 1
)
SELECT user_id, weight, log_time
FROM cte
WHERE rn = 1;
Here is an old school join way of doing this:
SELECT uw1.user_id, uw1.weight, uw1.log_time
FROM usr_weight uw1
INNER JOIN
(
SELECT DATE(log_time) AS log_time_date, MAX(log_time) AS max_log_time
FROM usr_weight
WHERE user_id = 1
GROUP BY DATE(log_time)
) uw2
ON uw2.log_time_date = DATE(uw1.log_time) AND
uw2.max_log_time = uw1.log_time
WHERE
uw1.user_id = 1;
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;
I have a table with non-unique column auth_id. I need to select the auth_id value with maximum number of entries.
SELECT auth_id, cnt
FROM (SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id) articles_num
WHERE cnt = (SELECT MAX(articles_num.cnt))
Here's the data example:
auth_id article_id
1 2
1 1
1 3
2 2
3 1
3 2
And the output:
auth_id cnt
1 3
But SQL doesn't see the alias table articles_num.
How do I make this WHERE clause with this alias?
Using a limit clause would be much simpler - you simply order a query according to some field, and then just take the first row:
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY 2 DESC
LIMIT 1
Order your data in descending order in your inner query then just take the first one:
SELECT auth_id, cnt
FROM (
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY cnt DESC
)
LIMIT 1
If I understand correctly, you actually want to get one row of the max of the count:
SELECT auth_id, count(auth_id) as cnt
FROM articles_authors
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1
If more than one auth_id have same max count, we need to update this SQL
Try this.......
select auth_id, count(auth_id) as cnt
from articles_authors
group by auth_id
order by cnt DESC
limit 1;
Let me know if it resolves your issue
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
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....