I've got one table, Classes, and another table that I call "courses" - that is, instances of a class taught by a specific person in a specific place. I want to pull a table that basically just shows me which Classes are active based on certain course parameters. For example:
CLASSES class_id|class_name --------|------------ 1|Class One 2|Class Two 3|Different Class etc... COURSES course_id|class_id|room ---------|--------|-------- 1| 3| 1 2| 3| 2 3| 1| 1 4| 3| 1 5| 3| 2 6| 2| 1 etc...
I'm wondering if there's a way that I can just get something like SELECT classes.* FROM classes JOIN courses ON classes.class_id=courses.class_id WHERE courses.room=1
to return only one instance of each class. What's happening is that I'm getting only the classes that take place in room 1, but I'm getting multiple instances of each because there are multiple instances of that class in the course table in room 1.
I've tried all different sorts of JOIN
- left, right, inner, etc. - and because I'm pulling from one table based on specifications from the other, they all appear to give me the same result.
So I'm getting:
class_id| class_name |course_id|room --------|--------------|---------|-------- 1|Class One | 3| 1 2|Class Two | 6| 1 3|Diferent Class| 1| 1 3|Diferent Class| 4| 1 etc...
But I want to just get:
class_id| class_name |course_id|room --------|--------------|---------|-------- 1|Class One | 3| 1 2|Class Two | 6| 1 3|Diferent Class| 1| 1 etc...
Can I have it only JOIN
on the first match for each row in Classes? I'm kind of new with MySQL so I'm having a little trouble expressing what I want to do clearly; I apologize.
Also: I'm pulling all of this into PHP via PDO
- maybe there's a specific way to accomplish this in PDO
?
The simplest kind of join we can do is a CROSS JOIN or "Cartesian product." This join takes each row from one table and joins it with each row of the other table. Each value from the first list is paired with each value of the second list.
SQL JOIN. A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.
The answer to this question is yes, you can join two unrelated tables in SQL, and in fact, there are multiple ways to do this, particularly in the Microsoft SQL Server database. The most common way to join two unrelated tables is by using CROSS join, which produces a cartesian product of two tables.
Ans: Joining two tables in SQL can be done in four major ways: Inner Join (returns rows with matching columns), Left Join (ALL records in the left table and matching records in the right table), Right Join (ALL records in the right table and matching records in the left table), and Union (removes duplicates).
SELECT
classes.class_id,
classes.name,
courses.room
FROM classes
JOIN courses
ON classes.class_id=courses.class_id
WHERE courses.room=1
GROUP BY classes.class_id,classes.name,courses.room
GROUP BY allows you to aggregate results on the fields specified, so (in this instance) it will take just the unique tuple of (classes.class_id,classes.name,courses.room)
for more details http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html
SELECT cl.class_id, cl.class_name, c.course_id, c.room
FROM classes cl
JOIN courses c
ON c.course_id =
(
SELECT ci.course_id
FROM courses ci
WHERE ci.class_id = cl.class_id
AND ci.room = 1
ORDER BY
ci.class_id, ci.course_id -- or whatever
LIMIT 1
)
By changing the ORDER BY
clause in the subquery, you can define which of the classes will be returned (that with the least or greatest id
etc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With