Retrieving date SQL with a join table - many-to-many

I have a small issue: I have a database with a join table similar to this:
I want to retrieve data to show the student name and the subject he is having.
I tried with an Inner Join between Student and Student Enrolment and added a new inner join between Student Enrolment and Subject but it didn't work.
SELECT student.student_name, subject.subject_name FROM Student
INNER JOIN subject_enrolment ON student.student_ID = subject_enrolment.student_id,
INNER JOIN subject ON subject_enrolment.subject_ID = subject.subject_ID;
Any ideas?

Related

how can i display the name of a professional from a patient's appointment?

I have this four tables:
One for users, other for professionals, other for patients y another for appointments. I want to get the name of professional from patient's appointment.
I try this:
SELECT appointments.date, appointments.start_time, appointments.indication, professionals.medical_box
FROM appointments
INNER JOIN patients ON appointments.id_patient = patients.id
INNER JOIN users ON users.id = patients.id_user
INNER JOIN professionals ON professionals.id = appointments.id_professional
WHERE users.id = 14
ORDER BY appointments.date DESC, appointments.start_time ASC
And I get all correctly, but when I try to show users.name, the name that I get is the patient name, no the professional name, I want the professional name from that patient appoinment. What will be the sql query?
It looks like to get the professionals name you need to also join the user table on the professional.
Add an alias for the second join onto the users table and reference that when displaying your fields.
SELECT appointments.date, appointments.start_time, appointments.indication, professionals.medical_box, usersProf .name
FROM appointments
INNER JOIN patients ON appointments.id_patient = patients.id
INNER JOIN users ON users.id = patients.id_user
INNER JOIN professionals ON professionals.id = appointments.id_professional
INNER JOIN users as usersProf ON usersProf .id = professionals.id_user
WHERE users.id = 14
ORDER BY appointments.date DESC, appointments.start_time ASC

Select values from two mysql tables

I have two tables called Job and Bid . customer can add job details and multiple suppliers can bid for the job.Once customer select one supplier from bids,that supplier id (sId) will update on Job table. i want to select job details with bid value.
This is what i tried
SELECT job.id,job.title,job.desc,job.mobile,job.address, job.city,job.updatedAt,job.status,bid.value,customer.cName,supplier.sName
FROM job
LEFT JOIN customer
ON customer.id = job.cId
LEFT JOIN supplier
ON supplier.id = job.sId
LEFT JOIN bid
ON bid.sId = job.sId`
but this query showing messy information with some duplicates
job Table
bid table
There are two suppliers (sId=2 and sId=1) has bid for job no 5 (i have marked on bid table).but customer has pick sId 2 for the job number 5.now i want to select from those tables without messy records.
I think that you are just missing a join condition on bid, that filters on the chosen supplier. The logic to allow jobs without a supplier yet while evicting jobs whose supplier was not picked by the customer is a bit tricky, and is implemented in the WHERE clause:
SELECT ...
FROM job
INNER JOIN customer
ON customer.id = job.cId
LEFT JOIN supplier
ON supplier.id = job.sId
LEFT JOIN bid
ON bid.sId = job.sId
AND bid.jId = job.id --> here
WHERE supplier.id IS NULL OR bid.id IS NOT NULL
in your select statment you tring to retrive data from bid table bid.value but you didn't join that table.
select j.id,
j.title,
j.desc,
j.mobile,
j.address,
j.city,
j.updatedAt,
j.status,
b.value,
c.cName,
s.sName
from job as j
inner join customer as c
on j.cId = c.id
inner join supplier as s
on j.sId = s.id
inner join bid as b
on j.id = b.jId;

Query code shows nothing after adding a new relationship

I have relationship between Student.Department and Departments.Id, Student.Faculty and Faculties.Id. Below code was working before I created a new relationship between Departments.facultyId and Faculties.Id
SELECT
*
FROM
Students sd
INNER JOIN Departments dp
ON sd.Department = dp.Id
INNER JOIN Faculties fd
ON sd.Faculty = fd.Id
Before adding facultyIdcolumn in Departments table, I could run the query, but now the query shows nothing.
Should I also write something for relationship between fd.Idand dp.facultyId?
I just replicate your database structure and the relationships. Your query working pretty well. If it doesn't work, make sure you have data to support your query. I mean in all tables since you are having left joins from faculty and department with student table. You can see the result for the query below.
SELECT * FROM student sd INNER JOIN department dp ON sd.dept = dp.id INNER JOIN faculty fd ON sd.fac = fd.id
Just to clarify it, I have 8 students (2 for each department), 4 facutlties (2 for each faculty) and 2 faculties. So it works. Check whether your DB has data to satisfy the query constraints.
First, verify this join:
SELECT
*
FROM
Students sd
LEFT JOIN Departments dp
ON sd.Department = dp.Id
If cannot see Departments information, verify that sd.Department has the correct values.
Do the same with Faculties
SELECT
*
FROM
Students sd
LEFT JOIN Faculties fd
ON sd.facultyId = fd.Id
Using LEFT JOIN you'll see all records of the first table, and only the records of second table that match the ON clause.
And finally check the new relation:
SELECT
*
FROM
Dapartments dp
LEFT JOIN Faculties fd
ON dp.facultyId = fd.Id

Semantics of multiple joins

What happens actually when we use cascaded join statements
select student.name, count(teacher.id)
from student
left join course on student.course_id = course.id
left join teacher on student.teacher_id = teacher.id
group by student.name;
It seems when I used only the first left join alone it returned 30 rows while using the second left join alone returned 20 rows. But using together returns 600 rows. What is actually happening ? Does the result from the first left join is used in the second ? I don't understand the semantics. Help me understand it.
Since you don't have any join conditions between teacher and course, you're getting a full cross-product between each of the other two joins. Since one join returns 20 rows and the other returns 30 rows, the 3-way join returns 20x30 = 600 rows. Its equivalent to:
SELECT t1.name, count(t2.id)
FROM (SELECT student.name
FROM student
LEFT JOIN course ON student.id = course.id) AS t1
CROSS JOIN
(SELECT teacher.id
FROM student
LEFT JOIN teacher ON student.id = teacher.id) AS t2
GROUP BY t1.name
Notice that the CROSS JOIN of the two subqueries has no ON condition.
The correct way to structure this database is as follows:
student table: id (PK), name
course table: id (PK), name, fee, credits
student_course table: id (PK), student_id (FK), course_id (FK), unique key on (student_id, course_id)
Then to get the name of each student and the average course fee, you would do:
SELECT s.name, AVG(c.fee) AS avg_fee
FROM student AS s
LEFT JOIN student_course AS sc ON s.id = sc.student_id
LEFT JOIN course AS c ON sc.course_id = c.id
All Mysql joins are graphically explained here. Take a look and choose correct joins for both joined tables.

SQL Query for multiple tables (foreign keys involved)

I currently have 5 tables in MySQL database. Some of them share foreign keys and are interdependent of each other. I am trying to create a query that will show all the results side by side(major, course, semester, etc). The query I created query it is not displaying my desired results since I have not added the other tables. I am not sure how to implement the other tables. How can I modify the mysql-query to display all the results in order?
Query
select * from course left join major on course.id = majors.id left join majors on courses_major_xref.majors_id = majors.id
Try the following
SELECT * FROM course
INNER JOIN major_courses_xref ON course.id = major_courses_xref.course_id
INNER join majors ON major_courses_xref.majors_id = majors.id
INNER JOIN courses_semester_xref ON course.id = courses_semester_xref.course_id
INNER JOIN semester ON courses_semester_xref.semester_id = semester.id;
I think there is just some order of operations problems in your query, try:
SELECT * from course
LEFT JOIN major_course_xref
ON course.id = major_course_xref.courseID
LEFT JOIN major
ON major.id = major_course_xref.major_id
LEFT JOIN course_semester_xref
ON course.id = course_semester_xref.course_id
LEFT JOIN semester
ON course_semester_xref.semester_id = semester.id