I want to search user_id of 1 in table below and get other user_ids related to same mid ( in this case mid of 1 & 3)
mid | user_id
1 1
1 2
1 3
2 2
2 3
2 5
3 1
3 5
3 2
The result must be :
mid | user_id
1 1
1 2
1 3
3 1
3 5
3 2
How is it done with MySQL query ?
Assuming I understood correctly, you want to first find all mid values that have a user_id value of 1, then get all user_id values from all those previously gotten mid values.
SELECT mid, user_id from table
where mid IN (SELECT mid FROM table WHERE user_id = 1)
Assuming that you want to get all rows for those mid that matches a user_id this should do what you want:
select * from your_table t1
where exists (
select 1 from your_table t2
where user_id = 1
and t1.mid = t2.mid
)
Sample SQL Fiddle
Result given your sample data:
| MID | USER_ID |
|-----|---------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 3 | 1 |
| 3 | 5 |
| 3 | 2 |
Related
I have 2 tables:
built_structures:
id | location_id | structure_id
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
ongoing_structures:
id | built_structure_id
1 | 1
2 | 1
Now I need get all data from ongoing_structures where built_structures.location_id = 1 so I expect record id = 1 from ongoing_structures but I have 2 records id 1 and 2 from built_structures
SELECT * FROM ongoing_structures os
JOIN built_structures bs ON (os.built_structure_id = bs.id)
WHERE bs.location_id = 1
How can I get records from ongoing depends from location ?
Update
My mistake thank you for help. Table should be like this
ongoing_structures
id | built_structure_id
1 | 1
2 | 3
I am dealing with a table Employee Complaint which has columns EmployeeId ComplaintSeverity and ComplaintByUser. ComplaintSeverity has four level 0,1,2, and 3.
So the table will look like this ,Example
ComplaintId|EmployeeId|ComplaintSeverity|usr_id
-----------------------------------
1 | 1 | 0 | 3
2 | 2 | 1 | 4
3 | 3 | 0 | 5
4 | 1 | 2 | 4
5 | 4 | 1 | 5
6 | 2 | 2 | 2
7 | 2 | 2 | 4
Any user can complaint employee with any of these level
When client search with severitylevel as 0,
The row should fetch as
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
3 | 3 | 0
for severitylevel as 1,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
5 | 4 | 1
for severitylevel as 2,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
4 | 1 | 2
6 | 2 | 2
EmployeeId 1 has been complained by 2 user with severitylevel 0,2 but his highest severity level is 2. so while searching for 0 level, 1 should not be displayed.
Can anyone help me?
The question was edited after the previous answer was submitted. The following would therefore be more accurate.
SELECT x.*
FROM my_table x
JOIN
( SELECT employeeid
, MAX(complaintseverity) severity
FROM my_table
GROUP
BY employeeid
) y
ON y.employeeid = x.employeeid
AND y.severity = x.complaintseverity
WHERE complaintseverity = 0 -- optional line
ORDER
BY employeeid;
You can try following query.
SELECT *
FROM
(
SELECT cs.`EmployeeId`, MAX(cs.`ComplaintSeverity`) severity
FROM ComplaintSeverity cs
GROUP BY cs.`EmployeeId`
) csdata
WHERE csdata.severity=1
Replace 1 with the severity level you want.
I have a table with columns ID, Content and Day. I am trying to find all rows that have duplicate Content and Day values and display all rows
SELECT ID,Content, `Day`, Count(*)
FROM table
GROUP BY Content,`Day`
HAVING COUNT(*) > 1
The current code will return a list of duplicate Content and 'Day' values for instance:
ID|Content|Day
1 | a | 1
2 | a | 1
3 | a | 1
4 | b | 2
5 | b | 2
6 | c | 3
7 | c | 4
Will result in:
ID|Content|Day|Count
1 | a | 1 | 3
4 | b | 2 | 2
But I want to display all the unique IDs as well;
ID|Content|Day
1 | a | 1
2 | a | 1
3 | a | 1
4 | b | 2
5 | b | 2
Just make a Sub-Query
select *
from table
where `day` in
(
SELECT ID
FROM table
GROUP BY Content,`Day`
HAVING COUNT(*) > 1
) A
Use that query as a subquery to join against the table again:-
SELECT table.ID, table.Content, table.`Day`
FROM table
INNER JOIN
(
SELECT Content, `Day`, Count(*)
FROM table
GROUP BY Content,`Day`
HAVING COUNT(*) > 1
) sub0
ON sub0.Content = table.Content
AND sub0.`Day` = table.`Day`
I have a table of hits for various users:
HITS:
id | userid
1 | 1
2 | 1
3 | 2
4 | 1
5 | 2
6 | 2
I want the fastest possible way to get a list of these items ranked by ID. So this:
HITS RANKED:
id | userid | ranks
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 1 | 3
5 | 2 | 2
6 | 2 | 3
I want to avoid joining two tables to each other, as this takes forever when the tables get big. Any other suggestions?
SELECT ID,
UserID,
Ranks
FROM
(
SELECT id,
userid,
#group:=CASE WHEN #temp <> userid THEN 1 ELSE #group+1 END AS ranks,
#temp:=userid AS clset
FROM (SELECT #group:= 0) s,
(SELECT #temp:= 0) c,
(SELECT * FROM hits ORDER BY userid, id) t
) x
ORDER BY ID
SQLFiddle Demo
I have a mysql problem. I have two tables like this that I need to join together.
table:
id otherid2
1 | 1
2 | 1
3 | 2
4 | 2
table2:
otherid otherid2
1 | 1
2 | 1
3 | 2
4 | 2
I'm using:
SELECT id,otherid FROM table INNER JOIN table2 ON table.otherid2=table2.otherid2
This gives me:
id otherid
1 | 1
1 | 2
2 | 1
2 | 2
3 | 3
3 | 4
4 | 3
4 | 4
As you can see I get duplicates of id as there is otherid2s that is not unique in table2. What I need is to INNER JOIN DISTINCT in some way, I only want the result to be as below. Not duplicates.
This is what I want:
id otherid
1 | 1
2 | 1
3 | 3
4 | 3
Can I do this in an easy way?
If you want the row with the lowest id in table2, this should probably do it
SELECT id, min(otherid)
FROM table
INNER JOIN table2
ON table.otherid2=table2.otherid2
GROUP BY id
In your comment you wanted the lowest, then I'd suggest a group by and a min aggregator
SELECT id, MIN(otherid) AS otherid ... GROUP BY id