I'm confused on how to write the following query in MySQL: Given one collaborator, I'd like to get all the collaborators that participated on each item that this collaborator worked on.
Here's my collaborators table:
id collaborator_id item_id
1 1 1
2 2 1
3 3 1
4 4 2
5 1 2
6 2 3
for collaborator_id=1, the query would return:
collaborator_id item_id
1 1
2 1
3 1
1 2
4 2
So collaborator_id=1 worked with collaborator_ids=2,3 on item_id=1 and worked by themselves on item_id=2.
This seems super simple but I'm having a brain freeze on how to get these results. Thoughts?
This query joins on the item_id and gets you all the unique collaborators who worked with a given collaborator on shared items, other than the collaborator himself:
SELECT distinct b.collaborator_id, b.item_id
FROM collab_table a
JOIN collab_table b
on a.item_id = b.item_id
WHERE a.collaborator_id = 1
and b.collaborator_id != 1
you can use subquery
SELECT *
FROM collaborator
WHERE item_ID IN
(
SELECT item_ID
FROM collaborator
WHERE collaborator_id = 1
)
ORDER BY item_ID, Collaborator_ID
or by using JOIN
SELECT a.*
FROM COLLABORATOR a
JOIN COLLABORATOR b
ON a.ITEM_ID = b.ITEM_ID
WHERE b.COLLABORATOR_ID = 1
ORDER BY item_ID, Collaborator_ID;
SQLFiddle Demo
What you need to do is join the table back to itself, using your filter on one of the aliases, and pulling the result set from the other:
select a.COLLABORATOR_ID, a.ITEM_ID
from COLLABORATOR as a
inner join COLLABORATOR as b on
a.ITEM_ID = b.ITEM_ID
where b.COLLABORATOR_ID = 1
Related
Here is my table deals_transaction_status_log with the following fields
id,user_id,deal_transaction_id,transaction_status_id in which i want to fetch the records with group by on transaction_id with latest record on(max id).
This table also join some another tables to get some other data.
Here is my query
SELECT dtsl.id ,dtsl.deal_transaction_id,dts.id as statusId
FROM deals_transaction_status_log as dtsl
JOIN deals_transactions as dt ON dt.id=dtsl.deal_transaction_id AND dt.visitor_id=140
JOIN DEALS as d ON d.idDeal=dt.deal_id
JOIN USER as u ON d.userId=u.idUser
JOIN deals_transaction_status as dts ON dts.id=dtsl.transaction_status_id
WHERE dtsl.user_id!=140 AND dtsl.transaction_status_id=14 AND dtsl.id IN (
SELECT MAX(dtsl.id)
FROM deals_transaction_status_log as dtsl
GROUP BY dtsl.deal_transaction_id
) GROUP BY dtsl.deal_transaction_id
This works fine however it returns only one record even if i have more than one record with same deal_transaction_id
for eg:
Sample Input
id user_id deal_transaction_id transaction_status_id
1 4 2 14
2 4 2 14
3 5 3 14
4 5 3 14
Result
id statusId deal_transaction_id
3 14 3
Expected result
id statusId deal_transaction_id
2 14 2
4 14 3
UPDATE
I just tried fa06 answer without any join
SELECT dtsl.id,dtsl.deal_transaction_id,dtsl.transaction_status_id FROM deals_transaction_status_log as dtsl
WHERE dtsl.id IN (
SELECT MAX(id)
FROM deals_transaction_status_log as b where dtsl.transaction_status_id=b.transaction_status_id)
AND dtsl.transaction_status_id=14
GROUP BY dtsl.deal_transaction_id
But still i am getting only one row
use correlated subquery
SELECT dtsl.id ,dtsl.deal_transaction_id,dts.id as statusId
FROM deals_transaction_status_log as dtsl
JOIN deals_transactions as dt ON dt.id=dtsl.deal_transaction_id AND dt.visitor_id=140
JOIN DEALS as d ON d.idDeal=dt.deal_id
JOIN USER as u ON d.userId=u.idUser
JOIN deals_transaction_status as dts ON dts.id=dtsl.transaction_status_id
WHERE dtsl.user_id!=140 AND dtsl.transaction_status_id=14 AND dtsl.id IN (
SELECT MAX(id)
FROM deals_transaction_status_log as b where dtsl.deal_transaction_id=b.deal_transaction_id)
I have two table in MySQL that looks as follows:
ID Name Information
1 A fsdf
2 B ada
3 A dsafd
4 A retret
5 C asdfsa
6 B xzc
and,
P_ID Name Loc_X Loc_Y
1 A 2 3
2 B 3 4
3 C 4 5
I would like to run a query in MySQL that return the result as follows:
NAME COUNT Loc_X Loc_Y
A 3 2 3
B 2 3 4
C 1 4 5
Currently, I am able to execute the following query:
SELECT Name,COUNT(*) as count FROM Table_A GROUP BY Name ORDER BY count DESC;
to get the following result:
NAME COUNT
A 3
B 2
C 1
I know that probably I can use this result to extract only the "Name" and fire another query to get the Loc_X and Loc_Y using PHP, but I was wondering whether there is a moe efficient way of doing it using DML. Is there a way to nest the queries?
Try something like this :
SELECT Table_A.Name,COUNT(*) as count, Table_B.Loc_X, Table_B.Loc_Y
FROM Table_A
INNER JOIN Table_B ON Table_A.name = Table_B.name
GROUP BY Table_A.Name, Table_B.Loc_X, Table_B.Loc_Y
ORDER BY count DESC;
I think you just need to join the tables on the common field here.
Try:
SELECT a.Name, count(a.id) AS Count b.Loc_X, b.Loc_Y
FROM Table_A a
INNER JOIN Table_B b ON a.Name = b.Name
GROUP BY a.Name
I am writing a query to grab the items that a specific user_id was the first to use. Here is some sample data -
item_id used_user_id date_used
1 1 2012-08-25
1 2 2012-08-26
1 3 2012-08-27
2 2 2012-08-27
3 1 2012-08-27
4 1 2012-08-21
4 3 2012-08-24
5 3 2012-08-23
query
select item_id as inner_item_id, ( select used_user_id
from test
where test.item_id = inner_item_id
order by date_used asc
limit 1 ) as first_to_use_it
from test
where used_user_id = 1
group by item_id
It returns the correct values
inner_item_id first_to_use_it
1 1
3 1
4 1
but the query is VERY slow on a giant table. Is there a certain index that I can use or a better query that I can write?
i can't get exactly what you mean because in your inner query you have sorted it by their used_user_id and and on your outer query you have filtered it also by their userid. Why not do this directly?
SELECT DISTINCT item_id AS inner_item_id,
used_user_id AS first_to_use_it
FROM test
WHERE used_user_id = 1
UPDATE 1
SELECT b.item_id,
b.used_user_id AS first_to_use_it
FROM
(
SELECT item_ID, MIN(date_used) minDate
FROM tableName
GROUP BY item_ID
) a
INNER JOIN tableName b
ON a.item_ID = b.item_ID AND
a.minDate = b.date_used
WHERE b.used_user_id = 1
I've got a budget table:
user_id product_id budget created
-----------------------------------------------------------------
1 1 300 2011-12-01
2 1 400 2011-12-01
1 1 500 2011-12-03
2 2 400 2011-12-04
I've also got a manager_user table, joining a manager with the user
user_id manager_id product_id
------------------------------------
1 5 1
1 9 2
2 5 1
2 5 2
3 5 1
What I'd like to do is grab each of the user that's assigned to Manager #5, and also get their 'budgets'... but only the most recent one.
Right now my statement looks like this:
SELECT * FROM manager_user mu
LEFT JOIN budget b
ON b.user_id = mu.user_id AND b.product_id = mu.product_id
WHERE mu.manager_id = 5
GROUP BY mu.user_id, mu.product_id
ORDER BY b.created DESC;
The problem is it doesn't pull the most recent budget. Any suggestions? Thanks!
To accomplish your task you can do as follows:
select b1.user_id,
b1.budget
from budget b1 inner join (
select b.user_id,
b.product_id,
max(created) lastdate
from budget b
group by b.user_id, b.product_id ) q
on b1.user_id=q.user_id and
b1.product_id=q.product_id and
b1.created=q.lastdate
where b1.user_id in
(select user_id from manager_user where manager_id = 5);
I'm assuming here that your (user_id, product_id, created) combination is unique.
For what it's worth, here's the code that returned what I was looking for:
SELECT DISTINCT(b1.id),mu.user_id,mu.product_id,b1.budget,b1.created
FROM budget b1
INNER JOIN (
SELECT b.user_id, b.product_id, MAX(created) lastdate
FROM budget b
GROUP BY b.user_id, b.product_id) q
ON b1.user_id=q.user_id AND
b1.product_id=q.product_id AND
b1.created=q.lastdate
RIGHT JOIN manager_user mu
ON mu.user_id = b1.user_id AND
mu.product_id = b1.product_id
WHERE mu.manager_id = 5;
Thanks for the help Andrea!
I have four tables: groups, users, votes, follows.
The structures of these tables are
groups
g_id g_title g_content
1 t1 content1
2 t2 content2
users
u_id u_groupid
1 1
2 1
3 2
4 2
5 2
votes
v_id v_userid v_groupid v_votes
1 1 1 1
2 1 2 1
3 2 2 1
4 3 2 1
5 3 1 1
follows
f_id f_userid f_groupid
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
The groups table records the basic information of a "group".
The users table keeps the relationship between users and groups, that is, if the user A belongs to groups B, then there will be a record in the user table.
The votes table means the supportive attitude that a user holds to a group.
If user A is interested in group A, then add a entry into the Follows table.
Now my problem is how to write one select statement to query the number of users, the number of votes and the number of followers of each group.
I want the query result likes this
g_id num_users unm_votes num_followers
1 2 2 3
2 3 3 2
By the way, my database is Mysql 5.0.51b.
If you want in 1 query, something like this will help you
SELECT g_id,
d1.num_users,
d2.unm_votes,
d3.num_followers
FROM groups gg
LEFT JOIN (SELECT g_id,
COUNT(u.u_id) AS num_users
FROM groups g
LEFT JOIN users u
ON u.u_groupid = g.g_id
GROUP BY g_id) d1
ON d1.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(v.v_userid) AS unm_votes
FROM groups g
LEFT JOIN votes v
ON v.v_groupid = g.g_id
GROUP BY g_id) d2
ON d2.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(f.f_userid) AS num_followers
FROM groups g
LEFT JOIN follows f
ON f.f_groupid = g.g_id
GROUP BY g_id) d3
ON d3.g_id = gg.g_id
GROUP BY gg.g_id
For the user count by group:
select g_id,count(u.uid) user_count from groups g, users u
where u.groupid = g.g_id
group by g_id
May want to read up on group by.