sql - MySQL Join Condition -
I'm having trouble in thinking in a way to get into MySQL I'm not sure who's for this job It would be most appropriate to include, so when someone tells it, I will edit the title. Here's the essence of what I am trying to do.
I have two tables, call a code sign
These are setup as follows,
student
only the ID field is unique
.---- + --------- -------- + ---- ---- + --------. | ID | Name | Parents Mark | + ---- + ----------------- + -------- + -------- + | 1 | Name goes here 1. 0 | 0 | | 2 | Name goes here 2 | 0 | 20 | | 3 | Name goes here 3 | 2 | 45 | 4 | Name goes here 4 | 2 | 50 | | 5 | Name goes here 3 | 1 | 20 | | 6 | Name goes here 1. 0 | 65 | .---- + ----------------- + -------- + --------.
symbol
the id and the name are unique
.---- + ----------------- + --------. | ID | Name | Ranking | + ---- + ----------------- + -------- + | 1 | Name goes here 1. 20 | | 2 | Name goes here 2 | 60 | | 3 | Name goes here 3 | 90 | | 4 | Name goes here 4 | 200 | | 5 | Name goes here 5 | 45 | 6 | Name goes here 6. 76 | .---- + ----------------- + --------.
Now, what I want is as follows.
1. I need to join the student on myself so that student
parent
= student
. ID
2. In the above example, I want to select only the line where student
Icon
(S2) that is the most under parent. In addition, only if student
. Join Mark
> = 20 (also S2)
3. I want to join the last student
. name
on symbol
. Name
(from S1), select the ranking.
Result
.---- + ----------------- + -------- + - - ----- + -------- + ---------- | ID | Name | Parents Children Mark | Ranking | + ---- + ----------------- + -------- + -------- + -------- + ---------- + | 1 | Name goes here 1. 0 | 5 | 20 | 20 | | 2 | Name goes here 2 | 0 | 4 | 50 | 60 | .---- + ----------------- + -------- + -------- + -------- + ----------
I think (?) It is possible to use a query, but not sure I
This query should do what you want.
SELECT S1.Id, s1.Name, s1.Parent, s2.Id as hair, max (s2.mark) as mark, m. Ranking S1 Inner Join Student S2 On (s1.id = s2.parent and s2.Mark & Gt; = 20) by the students join the left (s1.name = m.name) group on s1.Id, s1 .Name, s1.Parent, Child, Ranking;
Comments
Post a Comment