Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Join Condition

Tags:

sql

join

mysql

I'm having trouble thinking of a way to do the following join in MySQL. I'm not sure which joins would be best suited for this task, so I'll edit the title when someone points it out. Here's the gist of what I'm trying to do.

I have two tables, call one Students, and the other Marks.
They are setup as follows,

Students

Only the Id field is unique

  .----+-----------------+--------+--------.
  | Id | Name            | Parent |  Mark  |
  +----+-----------------+--------+--------+
  |  1 | Name goes here1 |     0  |     0  |
  |  2 | Name goes here2 |     0  |    20  |
  |  3 | Name goes here3 |     2  |    45  |
  |  4 | Name goes here4 |     2  |    50  |
  |  5 | Name goes here3 |     1  |    20  |
  |  6 | Name goes here1 |     0  |    65  |
  .----+-----------------+--------+--------.

Marks

Id and Name are unique

  .----+-----------------+--------.
  | Id | Name            |Ranking |
  +----+-----------------+--------+
  |  1 | Name goes here1 |    20  |
  |  2 | Name goes here2 |    60  |
  |  3 | Name goes here3 |    90  |
  |  4 | Name goes here4 |   200  |
  |  5 | Name goes here5 |    45  |
  |  6 | Name goes here6 |    76  |
  .----+-----------------+--------.  

Now, what I need is as follows.
1. I need to join Students on itself so that Students.Parent=Students.Id
2. In the above join I only want to select the row where Students.Mark (S2) is the highest under that parent. Also, only join if Students.Mark >= 20 (Also S2).
3. I want to join the previous Student.Name on Marks.Name (From S1), selecting the Ranking.

Result

  .----+-----------------+--------+--------+--------+----------.
  | Id | Name            | Parent |  Child |  Mark  |  Ranking |
  +----+-----------------+--------+--------+--------+----------+
  |  1 | Name goes here1 |     0  |     5  |   20   |     20   |
  |  2 | Name goes here2 |     0  |     4  |   50   |     60   |
  .----+-----------------+--------+--------+--------+----------.

I think(?) this is possible using one query, but am not certain.

like image 412
Ian Elliott Avatar asked Jul 23 '09 21:07

Ian Elliott


People also ask

Can we add a condition on join?

Usually a join condition is specified on the foreign key columns of one table and the primary key or unique key columns of another table. However, you can join on other columns as well. A join condition involves columns that relate two tables in some logical way. A join condition may involve more than one column.

How do you join tables in MySQL?

To join tables, you use the cross join, inner join, left join, or right join clause. The join clause is used in the SELECT statement appeared after the FROM clause. Note that MySQL hasn't supported the FULL OUTER JOIN yet.

What is join query in MySQL?

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

What is conditions to perform join operation?

Join operation combines the relation R1 and R2 with respect to a condition. It is denoted by ⋈. The different types of join operation are as follows − Theta join. Natural join.


1 Answers

This query should do what you are looking for.

SELECT 
    s1.Id, s1.Name, s1.Parent, s2.Id as Child, MAX(s2.Mark) as Mark, m.Ranking 

FROM 
    Students s1
    INNER JOIN Students s2 ON (s1.id = s2.parent AND s2.Mark >= 20) 
    LEFT JOIN Marks m ON (s1.name = m.name) 

GROUP BY 
    s1.Id, s1.Name, s1.Parent, Child, Ranking;
like image 69
sixthgear Avatar answered Sep 24 '22 01:09

sixthgear