lets say:
table users(id, name, ..)
table friends(uid, fid)
How can i get some 33456 user id friends?
SELECT fid idd, name
FROM users,
friends
WHERE uid = 33456
AND fid = id
But would give me unnexpected results
SELECT id, fid, name
FROM users,friends where users.id=friends.fid
WHERE users.id = 33456
SELECT id, fid, name
FROM users
INNER JOIN friends ON users.id=friends.fid
WHERE users.id = 33456
select id,name from users where id in (select fid from friends where uid=33465)
this will return all ID with the name of your friends.
SELECT b.fid, c.name
FROM users a INNER JOIN friends b ON a.id = b.uid
INNER JOIN users c on b.fid = c.id
WHERE a.id = 33456
SELECT u.name,u.id
FROM users u
WHERE u.id IN
(
SELECT f.fid
FROM users u INNER JOIN friends f ON u.id = f.fid
WHERE f.uid = 33456
)
Related
i have follwing tables
USERS :
* id
* name
* role id
* phone
STUDENTS
1: userid ( fk users.id)
2: course_id
STAFF
1: staff_id(fk users.id)
2: course-id
COURSES
1: COURSE_ID
1:COURSENAME
I need to find all accounts associated with a number
I need name,userid,roleid,courseid,corsename,staffid, by providing mobile
i have written this query but this return zero results
SELECT users.name, users.id, staff.user_id, students.course_id, users.role_id, courses.course_title FROM users INNER JOIN students ON users.id = students.user_id INNER JOIN staff ON staff.user_id = users.id INNER JOIN courses ON courses.id = students.course_id WHERE users.phone = '9495990028'
LIMIT 0 , 30
Try union operator:
SELECT name, s.userid, roleid, s.course_id, c.coursename, null
FROM USERS u
INNER JOIN STUDENTS s
INNER JOIN COURSES c
ON s.userid = u.id AND c.course_id = s.course_id
WHERE u.phone = ?
UNION
SELECT name, null, roleid, s.course_id, c.coursename, s.staff_id
FROM USERS u
INNER JOIN STAFF s
INNER JOIN COURSES c
ON s.staff_id = u.id AND c.course_id = s.course_id
WHERE u.phone = ?;
For the COURSES table, I check the id with the course_id from students and staff
SELECT name, s.userid, roleid, s.course_id, c.coursename, st.staff_id
FROM USERS u
INNER JOIN STUDENTS s ON s.userid = u.id
INNER JOIN STAFF st ON st.staff_id = u.id
INNER JOIN COURSES c ON c.course_id = s.course_id AND c.course_id = st.course_id
WHERE u.phone = ?
Table 'teams_members'
Team_id
User_id
Table 'users_playeraccounts'
User_id
Rank_solo
Summoner_name
Table 'users'
Id
Username
My target : Fetching the current team members with their summonername and rank_solo where rank_solo is the highest out of all their playeraccounts
So multiple entries in 'users_playeraccounts' can be belong to a single user.
Select user_id,summoner_name,rank_solo
from teams_members t
join users_playeraccounts p on t.user_id = p.user_id
join users u on u.id = p.user_id
where team_id = 103
With something added I have no clue of
try this
select a.username, u.rank_solo, u.summoner_name
from teams_members as t
inner join users_playeraccounts as u on t.user_id = u.user_id
inner join users as a on a.id in (select u.user_id from users_playeraccounts
group by u.summoner_name,
having max(u.rank_solo) );
I have 3 tables:
users
userid
username
Appointments
Appointid
useridfk
Appointname
Start
End
plinks
pid
useridfk
link
projectidfk
What i want to do is join all the table by the userid and display all the appointments where there is a link exists in plinks where the users username = Mike
Try something like this:
select b.* from users a join Appointments b on a.userid=b.useridfk join plinks c on a.userid=c.useridfk where a.username='Mike' and c.plinks is not null
Have you tried something like this?
SELECT *
FROM Appointments
INNER JOIN users
ON users.userid = Appointments.useridfk
INNER JOIN plinks
ON users.userid = plinks.useridfk
WHERE users.username = 'Mike'
AND plinks.link IS NOT NULL;
Guessing the relationships between the table I would do it this way:
SELECT appointments.*
FROM users
JOIN appointments ON users.userid = appointments.useridfk
LEFT JOIN plinks ON users.userid = plinks.useridfk
WHERE plinks.useridfk IS NOT NULL
AND users.username = 'Mike'
Try this
select a.* from Appointments a inner join Users b on b.UserId = a.UserIdFk
inner join PLinks c on b.UserId = c.UserIdFk
where b.UserName = 'Mike' and c.Link is not null
U can try this solved your problem...
SELECT * FROM Appointments as a INNER JOIN users as u ON u.userid = a.useridfk INNER JOIN plinks as p ON u.userid = p.useridfk
WHERE u.username = 'Mike' AND p.link IS NOT NULL;
I have four tables are users(id,name), roles(id,name), members(project_id,user_id,id), and member_roles(member_id, role_id). So how can I select users.name and roles.name from this tables simultaneously(project_id is the condition to select exactly the names what we need)?. I can select users.name and roles.name separately but when I connect two queries, my code was failed. This is the query to get users.name
select users.name from users
inner join members on members.user_id = users.id
where project_id = 1
and here is the query to get roles.name
select roles.name from roles
inner join member_roles on member_roles.role_id = roles.id
join members on members.id = member_roles.member_id
where project_id = 1
You're almost there. Also, to avoid ambiguity, supply an Alias on column Name.
SELECT DISTINCT
u.Name AS member_name,
r.Name AS role_name
FROM users u
INNER JOIN members m
ON u.id = m.user_id
INNER JOIN member_roles mr
ON m.id = mr.member_id
INNER JOIN roles r
ON r.id = mr.role_id
WHERE m.project_id = 1
How do you connect 2 queries? How about something like this:
select u.name as user_name, r.name as role_name from users u join members m on m.user_id = u.id join member_roles mr on mr.member_id = m.id join roles r on r.id = mr.role_id where m.project_id = 1;
I have two tables
Users: (id, name)
Relations: (user_id, relation_id)
User_id and relation_id are both ids from the table users.
What I want is ro recover all users who are friend with a specific user.
And here is my sql command: that doesn't work:
SELECT *
FROM users
NATURAL JOIN relations
WHERE user_id IN (SELECT id FROM users WHERE name='John doe');
Could you help me?
SELECT users.*
FROM relations
INNER JOIN users
ON relations.relation_id = users.id
WHERE relations.user_id = 12345;
You can also get the id with a subquery, just as you did in your example:
WHERE relations.user_id IN (SELECT id FROM users WHERE name='John doe');
To get all the relations a person has , the following query will work..
SELECT * FROM users us JOIN relations re ON us.id = re.relation_id
WHERE re.user_id = (
SELECT id
FROM users
WHERE name = 'John doe'
)
Try this
SELECT * FROM users as a
JOIN relations as b on a.id = b.user_id
WHERE b.user_id IN (SELECT id FROM users WHERE name='John doe')
SELECT user_id
FROM relations re
JOIN users us
ON us.id = re.user_id
WHERE relation_id = (
SELECT id
FROM users
WHERE name = 'John Doe'
)
Side note: you can't use NATURAL JOIN here, 'cause there is no column that have the same name and type in the two tables.
isnt this just a matter of querying the relations tables by the userId you are looking for?
select *
from relations
where user_id = #IdYouArelookingFor or relation_id = #IdYouArelookingFor
SELECT friend.*
FROM users AS friend
JOIN relations AS r
ON r.relation_id = friend.id
JOIN users AS u
ON u.id = r.user_id
WHERE u.name = 'John doe' ;
Friends are people with the same relation_id?
SELECT a.name FROM users a JOIN relations b on a.id = b.user_id
WHERE b.relation_id in
(select relation_id from relations where id = 'userid of user you are looking for')
AND a.id != 'userid of user you are looking for'
If your logic is different pls tell how it is working