Show latest ID and company ID based on job ID - mysql

Table: service
id companyid jobid
1 1 1
2 1 2
3 1 2
4 2 3
5 3 4
6 3 1
7 4 2
8 5 2
I type the following query:
SELECT *
FROM service
WHERE jobID = 2
ORDER BY companyID desc, ID desc
And I get the below output:
id companyid jobid
8 5 2
7 4 2
3 1 2
2 1 2
But I want my expected output is below:
id companyid jobid
8 5 2
7 4 2
3 1 2
How do I modify the query in order to get my expected output?

You should be able to just apply the max function along with a group by clause:
select max(id) id, companyid, jobid
from service
where jobid = 2
group by companyid, jobid
order by id desc
Given your sample data the result would be:
id companyid jobid
8 5 2
7 4 2
3 1 2

This should work:
select * from service s1
join (select max(id) as id
from service
group by jobid, companyid) s2 on s1.id = s2.id
where s1.jobid = 2--can comment to select all latest jobs

Related

MySQL join based on where from 2nd table

I have two tables as follows:
docs
id
carid
name
1
1
doc1
2
1
doc2
3
2
doc3
4
1
doc4
5
5
doc5
cars
carid
parentid
name
1
4
car1
2
5
car2
3
4
car3
4
4
car4
5
5
car5
Question: I want to write a query in mysql where I can pass the carid in where clause and get all the rows from docs table where the parentid is same as that of the passed carid.
Desired Outcome If I pass carid=3 then the rows 1,2,4 from docs table should be returned as the parentid is 4 for carids 1,3,4.
Simillarly, If I pass carid=2 then the rows 3,5 from docs table should be returned as the parentid is 5 for carids 2.5.
You need to join the cars-table twice. First for the condition and second for the parent:
select d.*
from cars c
join cars p on p.parentid=c.parentid
join docs d on d.carid=p.carid
where c.carid=3
You're thinking about this a little wrong in the aspect of a relational database .. You SHOULD have 4 tables:
docs
doc_id
name
1
doc1
2
doc2
3
doc3
4
doc4
5
doc5
cars
car_id
name
1
car1
2
car2
3
car3
4
car4
5
car5
cars_to_docs
car_id
doc_id
1
1
1
2
1
4
2
3
5
5
parents_to_car
car_id
parent_id
1
4
2
5
3
4
4
4
5
5
Then you could simply use a basic JOIN
SELECT b.doc_id FROM test.docs a
LEFT JOIN test.cars_to_docs b
ON a.doc_id = b.car_id
LEFT JOIN test.parents_to_car c
ON c.car_id = b.car_id
LEFT JOIN test.cars d
ON c.car_id = d.car_id
WHERE c.parent_id = (SELECT parent_id FROM test.parents_to_car WHERE car_id = 3)
This will give you your output of 1,2,4

Is it possible to select rows where the occurences of an id is > some value?

Given a table:
userid
activity
location
1
RoomC
1
2
RoomB
1
2
RoomB
2
2
RoomC
4
3
RoomC
1
3
RoomC
5
3
RoomC
1
3
RoomC
5
4
RoomC
1
4
RoomC
5
Im trying to select only the rows where a userid shows up more then X number of times, lets say >2, so in the above database, only rows for userid 2 and 3 would be selected
Would something like this work?
SELECT *, count(*)
FROM marktable
GROUP BY userid
HAVING count(*) > 1
This modified version of your query:
SELECT userid
FROM marktable
GROUP BY userid
HAVING COUNT(*) > 2
returns all the users that appear more than 2 times in the table.
Use it with the operator IN:
SELECT *
FROM marktable
WHERE userid IN (
SELECT userid
FROM marktable
GROUP BY userid
HAVING COUNT(*) > 2
);
See the demo.

MySQL: return first row from join

I have 2 tables "quizzes" & "users", I need to return list of first user took each quiz, with quiz_id:
tables structure
"quizzes" structure:
id name
1 England
2 france
3 Japan
4 USA
5 UAE
6 Sweden
7 Italy
8 Brazil
9 South Korea
10 India
"users" structure:
id user_id quiz_id
1 1 1
2 1 2
3 2 1
4 3 4
5 1 4
6 5 9
7 2 9
8 3 8
9 3 9
10 3 7
I need to run query to return first "user_id" took each "quiz", (order by users.id ASC)
expected results:
quiz_id user_id
1 1
2 1
4 3
7 3
8 3
9 5
thanks,
You first group by quiz and pick minimal id and then select based on those ids:
select quiz_id, user_id
from users
where id in(select min(id) from users group by quiz_id)
Select quiz_id, user_id
from users
Where (quiz_id, id) in
(
Select quiz_id, min(id) id
From users
Group by quiz_id
)

MySQL group-by with comma-seperated list

I have following data
id category_id bookmark_id
1 1 1
2 2 1
3 3 1
4 4 2
5 5 1
and mysql query is
SELECT * from table_name group by bookmark_id
it returns
id category_id bookmark_id
1 1 1
4 4 2
which is fine
But i want one more colum extra
id category_id category_list bookmark_id
1 1 1,2,3,5 1
4 4 4 2
Any idea how to achieve above result?
select min(category_id) as min_cat,
group_concat(distinct category_id order by category_id) as category_list,
bookmark_id
from your_table
group by bookmark_id

My sql query sum of highest points for each category

upload table
id category_id
1 1
2 2
3 3
4 1
In Ratings Table
id upload_id points
1 1 5
2 2 3
3 3 2
4 4 2
5 1 5
I want to display all the records from ratings table except id 4 because id 4 is the same category id 1
I excepted result
Uploaded_id 1 has sum of 10 points
Uploaded_id 2 has sum of 3 points
Uploaded_id 3 has sum of 2 points
Please help me.
Thanks In advance
Sasikumar
select upload_id, sum(points)
from rating r,
(select min(id) id from upload group by category_id) a
where a.id = r.upload_id group by upload_id