This question already has answers here:
Select last row in MySQL
(11 answers)
MySQL - Select the last inserted row easiest way
(9 answers)
Closed 5 years ago.
I have a table like this:
id transaction_id auto_recurring paid_amount package_customerid
37 0 1 0 4
45 37 1 0 4
51 0 1 0 4
57 51 1 0 4
62 0 1 0 4
67 62 1 0 4
There are 6 records of package_customer_id = 4. Now I want to get the last record of 4. in this case id = 67 is my desired record. I try this SELECT * FROM transactions GROUP BY package_customer_id. But I got first record of package_customer_id = 4. i.e: id = 4 is my fetched result. How can I get id = 67 (my desired record) modifying this sql?
SELECT * FROM transactions WHERE package_customer_id = 4 ORDER BY id DESC LIMIT 1;
That would be my shot at it. Sorry but i haven't tested it, i leave it up to you:)
EDIT:
Dont forget the quotes " ` " on columns name's:)
Check you column name package_customer_id OR package_customerid ?
You may try this .
SELECT temp.* FROM (SELECT * FROM `transactions` WHERE package_customer_id = 4 order by id DESC LIMIT 1 ) AS temp GROUP BY temp.package_customer_id
Don't use group by. Use where:
SELECT t.*
FROM transactions t
WHERE t.id = (SELECT MAX(t2.id)
FROM transactions t2
WHERE t2.package_customer_id = t.package_customer_id
);
You can filter this for whichever package customer ids you like, in the outer query.
You can use like this:
SELECT * FROM transactions WHERE id IN (SELECT MAX(id) FROM transactions GROUP BY package_customerid)
try this query
if you need with GROUP BY clause: use this query
SELECT * FROM transactions where GROUP BY package_customerid ORDER BY package_customerid DESC LIMIT 1;
OR
if you DON'T need with GROUP BY clause: use this query
SELECT MAX(id),transaction_id,auto_recurring,paid_amount,package_customerid FROM transactions where package_customerid=4;
Output:
id transaction_id auto_recurring paid_amount package_customerid
67 62 1 0 4
Related
id game_id user_id user_playing_status user_turn_status
1 1 2 1 1
2 1 4 1 0
3 1 6 1 0
How can we access previous record of current record ?
If there are three records r1,r2,r3 in table
so previous record of r2 should be r1 , r3 should be r1 and r1 should be r3.
I use the following query
select user_id
from current_playing_users
where id < (select id from current_playing_users where user_id = 2)
But i am not getting previous record of first record.I want to get records in anticlockwise manner. Like in image previous user_id of 2 should be 6
Are looking you for the correlated subquery ? :
select cu.*.
(select cu1.user_id
from current_playing_users cu1
where cu1.id < cu.id
order by cu1.id desc
limit 1
) as prev_user_id
from current_playing_users cu;
you could use LAG function
select LAG(user_id) over (order by {your desired order}) as previous_row
from ..
LAG function return the former cell at the desired column with a pre-defined order.
I have this table
**applications**
id user_id company_id shortlisted
1 10 99 0
2 10 100 1
3 10 101 1
4 10 102 0
5 11 99 1
6 12 99 0
6 12 101 0
What I want is to select all users
which have been shortlisted at-least once
which have not been shortlisted at all
For the first case, i have the following query:
SELECT user_id
from applications
where shortlisted=1
Group
By user_id
and this gives me the expected result like below
**applications**
user_id
10
11
But I'm trying the following query for the second case and it returns me an empty set:
Select user_id
from applications as Application
where shortlisted=0
and NOT EXISTS(Select user_id from applications where user_id=Application.user_id and shortlisted=1)
What am i missing?
PS: Please ignore any typos as i typed them manually for this post.
To get both results in a single query simply use aggregation:
select user_id, max(shortlisted) as was_shortlisted
from applications
group By user_id
You can use group by and having for both.
For the first:
select user_id
from applications
group By user_id
having max(shortlisted) = 1;
For the second:
select user_id
from applications
group By user_id
having max(shortlisted) = 0;
In all honesty, your version with the where is more efficient for the first query. This is just to show how closely related the queries are.
You can try following query;
select user_id from table1
group by user_id having MIN(shortlisted) = 1
This will give you to at least have shortlisted = 1 condition and don't have shortlisted = 0 records.
This question already has answers here:
Get latest updated records
(5 answers)
Closed 5 years ago.
I have an table called DemoTable contains some fields like com_name, updated_date, demo_var, I queried on it like
select * from DemoTable where demo_var=100;
so for example here i got 1000 records. On this 1000 record i want to query to get the last updated row of com_name based on updated_date
My table looks like
id demo_var com_name updated_date
1 100 XYZ 2017-11-10
2 100 XYZ 2017-11-09
3 100 ABC 2017-10-10
4 100 ABC 2017-10-11
5 150 AJD 2017-11-11
First I want to fecth where demo_var=100 and get the different com_name which are last before updated ones.
For example like
2 100 XYZ 2017-11-09
3 100 ABC 2017-10-10
I want these two records to be fetched.
To achieve second latest row per group you can use following query
select a.*
from demo a
where a.demo_var = 100
and (
select count(*)
from demo b
where b.demo_var = 100
and a.com_name = b.com_name
and case when a.updated_date = b.updated_date
then a.id > b.id
else a.updated_date < b.updated_date
end
) = 1 /* here 1 is for second last , 0 for latest and so on */
Note it compare rows by updated_date so if there are 2 rows for same updated_date and com_name then i have used id column to pick the second latest row and is assume that id column is set to auto increment by default
Demo
Following query will work:
select t.demo_var,t.com_name,max(t.updated_date)
from
(
select demo_var,com_name,upddated_date
from DemoTable
where demo_var=100
and (com_name,updated_date) not in (select com_name,max(updated_date)
from DemoTable
where demo_var=100
group by demo_var,com_name
)
)t
group by t.demo_var,t.com_name;
My Data is
ID SCORE
1 55
1 -1
1 25
1 -1
1 -1
1 35
2 25
2 -1
2 65
2 55
2 21
2 -1
Now i want to add/sum the score of each id ignoring -1 and i am trying with this code which is not working
SELECT SUM(CASE when SCORE>-1 THEN SUM(SCORE) ELSE 0 END)
FROM jbit WHERE htno='$id'
Here i am already using WHERE so how can i use another WHERE, if i use multiple Where in single query it may effect other processes.. please help me out
Help me out friends
if there is only two column then there is no need to use SUM, you can try below
SELECT id, IF(SCORE=-1,0,SCORE) AS scoreSum
FROM table1
GROUP BY id
Working DEMO
alternative ( not tested )
SELECT id, SUM(IF(SCORE=-1,0,SCORE)) AS scoreSum
FROM table1
WHERE htno =$id
GROUP BY id
SELECT SUM(score) AS score_sum
FROM jbit
WHERE htno='$id'
AND score <> -1 ;
SELECT SUM(`SCORE`)
FROM `jbit`
WHERE `htno` = '$id'
`SCORE` > 0;
Though, I'd suggest you to change '$id' to just $id if the column type of htno is INTEGER.
SELECT SUM(score) FROM <tablename> WHERE score != -1
I have a table in mysql, say table1.
I am running this on it:
SELECT FLOOR( MAX(id) * RAND()) FROM `table1`
This works well, but I am now trying to add a condition of "AND tom".
Where tom is a integer field.
For example:
id tom
1 0
2 3
3 2
4 0
5 0
6 3
7 1
8 1
9 3
etc.
So, my question is,
How can I pick a random value from id, which also satisfies tom='0' say?
SELECT id FROM `table1` WHERE tom = 0 ORDER BY RAND() LIMIT 1
This will first get all rows in which tom = 0,then order those results randomly. MySQL will then limit those results to just one, returning the single value you want to retrieve.
I hope I understood correctly:
SELECT id FROM `table1` WHERE tom = 0 order by rand() limit 1
select * from (
select * from table where tom = 0 ) as t order by rand() limit 1