sql inner join in many to many relationship - sql

i have a table users(user_id, username, password etc),
a table courses(course_id, coursename, etc)
and a table student_favouriteCourses(user_id, course_id) //both as primary key
and i would like if i know the id of the user, to search for all the courses that he has as favourites. can u help me with the join query please?

Maybe something like this:
SELECT
*
FROM
student_FavouriteCourse fav
JOIN student stu ON ( fav.user_id = stu.user_id)
JOIN courses crs ON ( fav.course_id = crs.course_id)
WHERE
fav.user_id = <yourvalue>
As an explanation:
1) this will return the student_FavouriteCourse (using "fav" as an alias) records you want
SELECT
*
FROM
student_FavouriteCourse fav
WHERE
fav.user_id = <yourvalue>
2) you then JOIN the student details (using "stu" as an alias)
JOIN student stu ON ( fav.user_id = stu.user_id)
3) you then JOIN the course details (using "crs" as an alias)
JOIN courses crs ON ( fav.course_id = crs.course_id)
Can I suggest that if you are new to JOINs and SQL, you run this query at each of steps above and then you will see how the join is working and how the resulting dataset is constructed.
EDIT: Read your post again and I see you don't actually need the student join, just the courses. Therefore:
SELECT
*
FROM
student_FavouriteCourse fav
JOIN courses crs ON ( fav.course_id = crs.course_id)
WHERE
fav.user_id = <yourvalue>

I think your trying a very basic MySQL query, can you please next time try to put your code in so we can help in your mistakes and you can learn from them?
Anyway the code youre trying to do should be something like this:
select u.id, u.username, c.coursename
from users u
left join student_favouriteCourses f on u.id = f.user_id
left join courses c on f.course_id = c.id
where u.username = '<YourUsername>'
This will return your user join with your favourite courses and then the course.
In case you want to make sure you show at least 1 course (confirm that course exist) inner join will help you:
select u.id, u.username, c.coursename
from users u
inner join student_favouriteCourses f on u.id = f.user_id
inner join courses c on f.course_id = c.id
where u.username = '<YourUsername>'
There are many ways to do those joins depends of your scenario and how you like to code that.
I leave you here a link that explains this much better.

Related

SQL Where on different table

SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id
Teacher_id being the session id,
I want to see all the students that have the same mentor.
Right now if I run this I just see all of the students twice, maybe one of you knows why?
My db scheme
You are not specifying on which columns you want to do the join, so you're getting a cross reference where all records are joined to all records.
You should do something like (not sure about your column names):
SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id

Access SQL LEFT JOIN not returning results

I have three tables: Comments, Users and CommentsHelpfulness.
Users can submit several comments, which are stored in the Comments table.
CommentsHelpfulness has three columns: UserID, CommentID and a "Helpful" Boolean. Every user can indicate for every comment if they find it useful, which will create an entry in the CommentsHelpfulness table.
I want a query that gives me all Comment IDs, with the name of the user that submitted it and shows whether the currently logged in user found it helpful, did not find it helpful or did not say anything about it. So the ID of a comment the current user did not express his opinion about should still be output, just without the helpful Boolean.
To me that sounds like it should be done like this using a left join:
SELECT Comments.ID, Users.Nom, CommentsHelpfulness.Helpful
FROM (Comments INNER JOIN Users
ON Comments.UserID = Users.ID)
LEFT JOIN CommentsHelpfulness
ON (CommentsHelpfulness.CommentID = Comments.ID
AND (CommentsHelpfulness.UserID = ?))
Unfortunately this does not output Comment IDs without an entry in the CommentsHelpfulness table. Why does this not work? Is it because of Access?
I think the issue is the inner join, not the left join.
Try removing that table:
SELECT c.ID, ch.Helpful
FROM Comments as c LEFT JOIN
CommentsHelpfulness as ch
ON ch.CommentID = c.ID AND
ch.UserID = ?
select c.id, c.Nom, d.Helpful from (
select a.ID, b.Nom from Comments a left join Users b
on a.UserID = b.ID) c left join CommentsHelpfulness d
on d.CommentID = c.ID;

sql - How to have multiple select/from statements in one query

I'm trying to pull a report where each column is selecting from a specific table set. However, one of the columns needs to pull from a completely different table set and still be included in the same report. Of course, this doesn't work:
select u.first_name, ticket_work.time_spent
FROM tickets LEFT OUTER JOIN ticket_work ON ticket_work.ticket_id = tickets.id JOIN users u
(select count(tickets.id) FROM tickets JOIN users u)
where tickets.assigned_to = u.id
...
So just the part (select count(tickets.id) FROM tickets JOIN users u) needs to be selecting from the different table set but still be included in the report.
I'm a little confused by your question. Are you wanting to return the user, the count of tickets for that user, and the amount of time spent overall? If so, something like this should work:
select u.id, u.first_name,
SUM(tw.time_spent) summed_time_spent,
COUNT(DISTINCT t.id) count_tickets
FROM users u
LEFT JOIN tickets t
ON u.id = t.assigned_to
LEFT JOIN ticket_work tw
ON tw.ticket_id = t.id
GROUP BY u.id, u.first_name
Your questions is unclear, but just generally, it sounds like you're trying to join to a derived table (i.e., a query). In that case, do this:
SELECT...
FROM...
table_A A LEFT JOIN
(SELECT keyfield, valuefield FROM table_b WHERE ...) B
ON A.keyfield = B.keyfield
Does that make sense? To make a derived table, you put a query inside of parenthesis, give it an alias ('B' in this case), and then join it to your other tables as though it were a regular table.
Don't know about your table structure but you may use a sub query for such requirement
select u.first_name, ticket_work.time_spent,(select count(tickets.id) FROM tickets where ticket.id=ticket_work.ticket_id) as myCount
FROM tickets LEFT OUTER JOIN ticket_work ON ticket_work.ticket_id = tickets.id JOIN users u
where tickets.assigned_to = u.id

SQL Query - Multiple Joins on Same field

I need assistance building this query where i need to select different values from same table but different Unique Keys.
To elaborate more ill provide the below example:
I have 2 tables:
Issues (IssueID, AuthorID_FK, AssigedID_FK, ... )
Users (UserID, User_Label, ... )
Both AuthorID_FK & AssigedID_FK are linked to table Users and i need to get in the same query result the User_Label for both.
Can you please assist?
Thanks,
SELECT a.IssueID, b.User_Label, c.User_Label FROM Issues a
INNER JOIN USERS b on a.AuthorID_FK = b.UserID
INNER JOIN USERS c on a.AssignedID_FK = c.UserID
something like that :) This should work in MS SQL Server
Well, this one should work too :)
SELECT IssueID, U.User_Label FROM Issues I
INNER JOIN Users U ON U.UserID = I.AuthorID_FK
UNION
SELECT IssueID, U.User_Label FROM Issues I
INNER JOIN Users U ON U.UserID = I.AssigedID_FK
Wont
SELECT a.IssueID, b.UserID
FROM Issues a
JOIN Users b ON (a.AuthorID_FK=b.UserID OR a.AssignedID_FK = b.UserID)
work?
You may want to try something like this
SELECT
issues.IssueID,
Authour.User_Label AS Author_Label,
Assigned.User_Label AS Assigned_user_Label
FROM
issues
INNER JOIN users AS Authour ON Authour.UserID = issues.AuthorID_FK
INNER JOIN users AS Assigned ON Assigned.UserID = issues.AssignedID_FK

SQL Query - Join

I have a Table which contains student information(name, id, course,....), and I have another table which contains information like(id, student leave)
I need to generate a report, which is by course all the students who were absent....
Am confused on how to do this properly - Do i use an outer join or what cause SQL 5.0 keeps giving me errors like syntax error....
Have to use this query in servlets...!!!
Currently am using the query in 2 parts.... but which is not generating the table properly....
SELECT * FROM Student s LEFT JOIN Attendance a ON a.student_id = s.id WHERE s.course_id = "ENG"
SELECT *
FROM Student s, Attendance a
WHERE s.id = a.student_id
AND s.course_id = "ENG"
AND s.id = <the_retrieved_id>
Should also be possible.
I suggest turning Student.id to Student.student_id to make it clear,
and also use integer for keys in course table.
then use join like Pradeep Singh only with "using":
select * from Student s left join Attendance a using (student_id) where s.course_id="ENG"
Select * From Student s left join Attendance a on a.student_id = s.id where course_id='ENG'