How to join three tables that linked to one table in MySQL? - mysql

How do I join three tables that are all linked to one table?
For example, I need to join a term table, a student table and a course table in a University db.
All these tables are linked to just the section table and none other table, but I need to retrieve data from all three tables.

You will have a key relationship between these tables.
ex: you will have a courseid in Course table and the same as a foreign key reference in Student table.
You should decide on what type of join(INNER, OUTER) you need.
From your requirement:
You need sum of the number of credits selected for each term by students. For this you can use the below query.
select selection.student_id, selection.course_id, selection.term_id, sum(course.credits) from selection
join student on selection.student_id = student.id
join course on selection.course_id = course.id
join term on selection.term_id = term.id
group by selection.student_id, selection.course_id, selection.term_id

Related

Finding voters that did not vote in an election through a many to many relationship with MySQL

I have two tables linked through a many-to-any relationship. Table one contains all voters with an ID, table two contains all elections with an ID, and table three links both tables using their IDs.
Not all voters voted in all elections. I would like to query the many-to-many relationship to find the elections for each voter that they did not vote in. I'm using MySQL.
A typical solution to this is to generate all possible combinations of voters and elections with a cross join, then try to bring the junction table with a left join: where no record matches, you know a voter missed an election.
Consider:
select v.voter_id, e.election_id
from voters v
cross join elections e
left join voter_elections ve
on ve.voter_id = v.voter_id
and ve.election_id = e.election_id
where ve.voter_id is null

Is it really necessary to create foreign key in this query?

I have a table called investors having a primary key as ID. I also have additional tables named Users Login_Logs and Accounts. All of these tables contain the foreign key investor_id. Between investors and Users we have a one to one relation. between investors and Login_Logs a one to many relation and between investors and Acccounts a one to many relation as well. My question is in order to create a query that loads info contained in the table Users, Login_Logs and Accounts - do I need to store the Users id, Accounts id and Login_Logs id in the investors table? I mean, do I need to create foreign keys in the investors table for all columns?
No, foreign key constraints don't affect joins and read queries. They ensure that the values in the child column(s) exist in the referenced column(s). They're used for integrity, not for linking rows or tables.
AFAICT recording the user id in your investors table is redundant, and recording the account and login_log ids in investors isn't practical.
To be able to join the tables efficiently, what you need is to index the investor_id in each of the tables. Then, it's up to your query to connect the tables as required.
The problem with retrieving all the information about investors at the same time is that you have (multiple) one-to-many relations. If we join all the tables:
SELECT *
FROM investors i
JOIN users u ON i.ID = u.investor_id
JOIN accounts a ON i.ID = a.investor_id
JOIN login_logs l ON i.ID = l.investor_id
Then, if an investor has 2 accounts and 2 login_logs, then we'll get 4 rows. SQL databases can't nest related data. Instead, you may have to use 3 queries to retrieve everything about investors:
SELECT *
FROM investors i
JOIN users u ON i.ID = u.investor_id
SELECT *
FROM accounts
SELECT *
FROM login_logs
Then process the results in code. You could process the combined query above programmatically, but it's a bit more complicated.

How to get row only if column value equals "joined_table1.id" or "joined_table2.id"?

I have 2 different tables that hold different types of products, e.g. Phones or TVs. Each of those two tables have a PK.
I also provide a catalog functionality, where a single catalog may contain multiple TVs and multiple Phones. In the catalog_items.idItem column I am storing the PK of the item being added to the catalog (but not its type).
My question is, how do I retrieve all catalog_items rows where the item is present in at least one of the two product tables, by just knowing the ID that is joining the three tables?
To visualize:
Table tvs:
id
Name
Table phones:
id
Name
Table catalog_items:
id
idItem (may contain a ID from either tvs.id or phones.id, but also may be a different product entirely; I want to exclude rows with IDs of other products besides TVs and Phones)
First, are you sure you don't have duplicate key values in the two tables? IOW, table tv.id can not exist in phones.tv.
SELECT
a.id as cat_id,
b.id as tv_id,
b.name as tv_name,
c.id as phone_id,
c.name as phone_name
FROM catalog_items a
LEFT JOIN tvs b
ON a.iditem = b.id
LEFT JOIN
ON a.iditem = c.id
WHERE NOT(b.id IS NULL) AND NOT(c.id IS NULL)
Not the best way to store the data, or the most efficient way to have to retrieve it. It would be much better to have all of the products in one table.
One alternative would be to create a view that unions the product tables, and then use the view in a simple two table inner join. This would allow you to order the results by any of the fields in the product tables.

Access 2013 multi value field

I'm creating a table for a college major. The table is called major. The columns will be majorID, majorName, and requiredCourses.
In Access how can I make requiredCourses a multivalue field? Required courses will be around 20 courses.
Thank you for your help.
You need to create a one-to-many relationship. The way is is usually done is like this:
You need to create a new table for the courses. Call it course. The table will contain CourseID, CourseName, etc. CourceID will be a primary key of this table
You will need to create another table that will act as a link between your major and your course tables. The table can be called something like majorCourses. The table will contain at least these two fields: majorID and courseID (you can of course add more fields, like dateAdded, isInactive, etc.).
To link your tables you will need to JOIN these tables, like this:
SELECT m.majorID, m.majorName, c.courseID, c.CourseName
FROM major m
INNER JOIN (majorCourses mc INNER JOIN course c ON mc.courseID = c.courseID)
ON m.majorID = mc.majorID

MySQL Attendance System , Problems with joining tables, duplicate id's for 1 primary key

Ok So I am creating a Attendance System for a College and I am having trouble doing the tables in MySQL
The Problem I'm having is Grouping a group of students together for one class. So in MySQL
TABLE NAME: CourseGroup: courseGroup_id , student_id(FK), course_id(FK)
I want a number of students belonging to one class, this in hindsight will enable a teacher to undertake a register for that class group
In MySQL I have set the 'courseGroup_id as a unique field, however when I try to link 'courseGroup_id' in the Attendance table it only enables me to select 'student_id' and 'course_id'
Is it possible to select the courseGroup_id and display students for one class
My Attendance table looks like this:
Attendance: week_number, day, time, courseGroup_id, present
I want to be able to view all the students that belong to one class
I’m a bit lost with your terminology, you speak of “class id” but don’t reefer to that in your question – instead you use “course”.
I think the best option you can do here is create another table enabling a many to many relationship as many students can be registered to one or many courses. This type of table will enable a mapping between students and courses.
For example, if you create the mapping table “student_courses” with
student_id INT
course_id INT
You can then select students by course (joining the student table)
SELECT * from student_courses sc
INNER JOIN students s
ON s.student_id = sc.srudent_id
WHERE sc.course_id = ?CourseId
You can also reverse it and show the courses by for an individual course:
SELECT * from student_courses sc
INNER JOIN courses cs
ON cs.courset _id = sc.course_id
WHERE sc.student_id = ?StudentId
It is also worth pointing out that there should be two index on the student_courses, indexing both student_id and course_id for performance reasons.