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
Related
I am trying to display the driver's first and last name but when I run this query the name columns just return test and not the actual names from the employee table
SELECT checklistitem.*,
m1.Company_ID AS Company_ID,
m1.ChecklistID As ChecklistID,
e.FirstName As FirstName,
e.LastName As LastName
FROM checklistitem
LEFT JOIN employee e ON e.CompanyID
LEFT JOIN maintenance m1 ON m1.CheckListID
LEFT JOIN Vehicle v ON m1.LinkedID = v.ID
LEFT JOIN Trailer t ON m1.LinkedID = t.ID
WHERE m1.Company_ID = 129
I thought maybe the table wasn't linking correctly to find the names so I tried changing the code to LEFT JOIN employee e ON m1.Company_ID because both the maintenance and the employee table have a company ID but I get the error
Unknown column 'm1.Company_ID' in 'on clause'
Your issue is with the LEFT JOIN statements. You have an invalid syntax in your ON clause.
FROM checklistitem
LEFT JOIN employee e ON e.CompanyID /*ERROR*/
LEFT JOIN maintenance m1 ON m1.CheckListID /*ERROR*/
Your SQL statement has to be table.column = table2.column
So I'm gonna go out on a guess and say you should being using
FROM checklistitem
LEFT JOIN employee e ON checklistitem.CompanyID = e.CompanyID
LEFT JOIN maintenance m1 ON e.CheckListID = m1.CheckListID
Please note: I don't know what your tables are setup like. checklistitem.CompanyID and e.CheckListID was a guess. Please replace those with the correct fields from your tables.
Also with your table names, m1.Company_ID will automatically turn into Company_ID. No need to use AS.
I am completing a piece of work and one of the team members who was in charge of SQL has gone on holiday.
I have the following query:
SELECT
Car.*,
Building.*,
CarType.*
FROM
Car
INNER JOIN Building ON
Car.BuildingID=Building.BuildingID
INNER JOIN CarType ON
Car.CarType=CarType.TypeID
this is returning all of the information we need it to about the cars and buildings, however, we need to edit the query so depending on the value of a field in the Request table, called status, if this field is not = 'Accepted', then do not display the information about the buildings or cars?
I have tried the following:
SELECT
Car.*,
Building.*,
CarType.*,
Requests.*
FROM
Car
INNER JOIN Building ON
Car.BuildingID=Building.BuildingID
INNER JOIN CarType ON
Car.CarType=CarType.TypeID
WHERE
Requests.Status <> 'Accepted'
However this does not work
any help would be appreciated
thank you
You are missing the Requests table from your Query. Assuming that there is a field RequestID in the Car Table your query should look like
SELECT Car.*, Building.*, CarType.*
FROM Car
INNER JOIN Building ON Car.BuildingID=Building.BuildingID
INNER JOIN CarType ON Car.CarType=CarType.TypeID
INNER JOIN Requests ON Car.RequestID = Requests.RequestID
WHERE Requests.Status = 'Accepted'
Notice I have joined the Requests table to the Car table Assuming both contain a field called RequestID
Following Query Will return only Car , Building and cartype information Where status is only 'Accepted' not other then
SELECT Car.*, Building.*, CarType.*
FROM Car INNER JOIN Building
ON Car.BuildingID=Building.BuildingID INNER JOIN CarType ON
Car.CarType=CarType.TypeID
/* add here joining of Requests Table */
WHERE Requests.Status = 'Accepted'
Update:
/* add one line from below lines into the just below where Clause
as per your Table hierarchy */
INNER JOIN Requests.ReqID= Car.ReqID
INNER JOIN Requests.ReqID= Building.ReqID
INNER JOIN Requests.ReqID= CarType.ReqID
I have the following database example:
The example is pretty much self-explanatory: There are lessons held by teachers at defined time periods (time_start, time_end) each time period -> lesson connection has its own max_students number.
I know want to list all lessons with all information of the 3 tables (and the max_students). I would do it like that (I heard, that joining table like that is the fastest way):
SELECT * FROM lesson, teacher, time, teacher_has_lesson, time_has_lesson
WHERE lesson.lesson_id = teacher_has_lesson.lesson_lesson_id
AND teacher.teacher_id = teacher_has_lesson.teacher_teacher_id
AND lesson.lesson_id = time_has_lesson.lesson_lesson_id
AND time.time_id = time_has_lesson.time_time_id
1.) Is this a good solution if you just want to join 3 tables or are there better ones?
2.) This SQL call will get me only lessons, that have a teacher and a time. I also want to display lessons, that are in the database, but dont have a teacher or time yet. How can I do that?
There's an alternative way of writing this using join syntax. What you have is equivalent to an inner join, where you only see rows where there are matches:
select
*
from
lesson l
inner join
teacher_has_lesson tl
on l.lession_id = tl.lesson_lesson_id
inner join
teacher t
on tl.teacher_teacher_id = t.teacher_d
inner join
time_has_lesson tml
on l.lesson_id = tml.lesson_lesson_id
inner join
time tm
on tml.time_time_id = tm.time_ud
There's another type of join called outer join, where all the rows from one table are shown, and null values supplied if there are no matching values in the other table. It comes in two or three variants. left outer join shows all rows from the first table. right outer join shows all rows from the second table. full outer join shows all rows from both tables. So, for your second query you could use:
select
*
from
lesson l
left outer join
teacher_has_lesson tl
on l.lession_id = tl.lesson_lesson_id
left outer join
teacher t
on tl.teacher_teacher_id = t.teacher_d
left outer join
time_has_lesson tml
on l.lesson_id = tml.lesson_lesson_id
left outer join
time tm
on tml.time_time_id = tm.time_ud
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
To filter a table output of selected entries from a single table i would need something like a multiple JOIN request through several tables.
I want to filter a table of people by a special column in the table. Lets say this column is "tasks." Now tasks is also another table with the column "people" and the values between those two tables are connected with an existant "join" table in the database, which is matching several IDs of one table to each ID of the other table.
Now if this would be simple as that i could just filter with an INNER JOIN and a special condition. The problem is, that the entries of the table "tasks" are connected to another table over a "join" table in the database. To simplify things lets say it is "settings". So each "task" consists of several "settings" which are connected via a join table in their IDs.
So what is the input?
I got an array of IDs, which are representing the settings-ids i do not want to be shown.
What should be the output?
As already said i want a filtered output of "people" while the filter is "settings."
I want the sql request to return each entry of the table "people" with only joined tasks that are not joining any of the "setting-ids" from the array.
I hope you can help me with that.
Thanks in advance!
Example
Settings-Table:
1. Is in progress
2. Is important
3. Has unsolved issues
Tasks-Table: (settings.tasks is the join table between many tasks to many settings)
1. Task from 01.01.2012 - JOINS Settings in 1 and 3 (In progress + unsolved issues)
2. Task from 02.01.2012 - JOINS Settings in 2 (Is important)
3. Task from 03.01.2012 - JOINS Settings in 1 and 2 (...)
People-Table: (people.tasks is the join table between many people to many tasks)
1. Guy - JOINS Tasks in 1, 2, 3 (Has been assigned to all 3 tasks)
2. Dude - JOINS Tasks in 1 (Has been assigned to the Task from 01.01.2012)
3. Girl - JOINS Tasks in 2, 3 (...)
Now there is an array passed to a sql query
[2,3] should return noone because every person is assigned in a task that was either important or had unsolved issues!
[3] would return me only the person "Girl" because it is the only one that is assigned to tasks (2 and 3) that had no unsolved issues.
I hope it is clear now. :)
SELECT DISTINCT PEOPLE.*
FROM PEOPLE INNER JOIN PEOPLE_TASKS ON PEOPLE.PERSON_ID = PEOPLE_TASKS.PERSON_ID
WHERE TASK_ID NOT IN (SELECT DISTINCT TASK_ID
FROM TASK_SETTINGS
WHERE SETTING_ID = <Id you don't want>)
EDIT (for supplying multiple setting ids you don't want)
SELECT DISTINCT PEOPLE.*
FROM PEOPLE INNER JOIN PEOPLE_TASKS ON PEOPLE.PERSON_ID = PEOPLE_TASKS.PERSON_ID
WHERE TASK_ID NOT IN (SELECT DISTINCT TASK_ID
FROM TASK_SETTINGS
WHERE SETTING_ID IN (<Id you don't want>))
First you have to join table people and table tasks with the join table, let's call it people_tasks.
select distinct p.* from people p
inner join people_tasks pt on p.people_id = pt.people_id
inner join tasks on t.tasks_id = pt.tasks_id
Then you have to join table tasks and table settings with the join table, let's call it tasks_settings. You have to join them in the current select.
select distinct p.* from people p
inner join people_tasks pt on p.people_id = pt.people_id
inner join tasks on t.tasks_id = pt.tasks_id
inner join tasks_settings ts on t.tasks_id = ts.tasks_id
inner join settings s on s.settings_id = ts.settings_id
and now you have all people connected with its tasks and its settings. Finally you need the restriction. With the people with the settings selected, you choose the others like this:
select distinct p.people_id from people p
inner join people_tasks pt on p.people_id = pt.people_id
where p.people_id not in (
select distinct p2.people_id from people p2
inner join people_tasks pt2 on p2.people_id = pt2.people_id
inner join tasks t2 on t2.tasks_id = pt2.tasks_id
inner join tasks_settings ts2 on t2.tasks_id = ts2.tasks_id
inner join settings s2 on s2.settings_id = ts2.settings_id
where s2.settings_id in (list of ids)
)