I have these two tables A and B:
Table A:
|--------------|----------------|
| ID_A |other attributes|
|--------------|----------------|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
|--------------|----------------|
Table B:
|--------------|----------------|----------------|----------------|
| ID_B | ID_A | update_time |other attributes|
|--------------|----------------|----------------|----------------|
| 1 | 2 |2017/01/01 07:00| |
| 2 | 2 |2017/01/01 11:00| |
| 3 | 2 |2017/01/01 13:00| |
| 4 | 2 |2017/01/01 08:00| |
| 5 | 2 |2017/01/01 06:00| |
| 6 | 3 |2017/01/01 12:00| |
| 7 | 3 |2017/01/01 13:00| |
| 8 | 4 |2017/01/01 17:00| |
|--------------|----------------|----------------|----------------|
Now I want to get the latest update time (from table B) for every row in table A. If there is no relation between table A and table B, I want to show NULL. The desired result for the upper tables is:
|--------|----------------|
| ID_A | update_time |
|--------|----------------|
| 1 | NULL |
| 2 |2017/01/01 13:00|
| 3 |2017/01/01 13:00|
| 4 |2017/01/01 17:00|
|--------|----------------|
Is there any way how to do this in SQL? Also some explanation would be fine. Thanks for any help!
Use a LEFT JOIN. This will return ONLY the matching values from Table B and keep all the values from Table A.
The subquery of LEFT JOIN will Return ALL values with ID_A and their MAX value of Update_DateTime.
Select a.ID_A
b.Update_Time
FROM TABLE A A
LEFT JOIN (Select ID_A, max(Update_Time) as Update_Time
FROM TABLE B
Group by ID_A) B on B.ID_A = A.ID_A
Related
I have multiple tables (tbldept, tblcourse, tblstud, tblviolation) and I want to extract specific values. The below tables is like the same on my tables
tbldept
id | dept
1 | deptA
2 | deptB
tbldept has foreign key on tblcourse
tblcourse
id | deptId | course
1 | 2 | courseA
2 | 1 | courseB
3 | 1 | courseC
tblcourse has foreign key on tblstud
tblstud
id | courseId | name
1 | 1 | studA
2 | 2 | studB
3 | 1 | studC
tblstud has foreign key on tblviolation
tblviolation
id | studId | violationName
1 | 3 | violationA
2 | 2 | violationB
3 | 1 | violationC
4 | 3 | violationC
*What I want to get is look like this: *
dept | studId | name | violationName
2 | 1 | studA | violationC
2 | 2 | studB | violationB
1 | 3 | studC | violationA
1 | 3 | studC | violationC
I want to get all the rows of tblviolation for each studId.
I hope you guys understand what I am trying to explain. =) Thank you.
You just need inner join. Try this.
select d.dept,s.studid,s.name,v.violationname
from tbldept d
inner join tblcourse c
on d.id=c.deptid
inner join tblstud s
on c.id=s.courseid
inner join tblviolation v
on s.id=v.studid
How can i achieve this in mysql?
Result table
+-----------------+-----------------------+------+
|st-id | term | score|
+-----------------+-----------------------+------+
| 20001 | 1 | 5 |
| 20002 | 1 | 6 |
| 20001 | 2 | 6 |
| 20002 | 2 | 4 |
| 20003 | 1 | 7 |
| 20003 | 2 | 9 |
+-----------------+-----------------------+------+--
Result query should be like this
+--------------------------------+
|st-id | score->term 1 | score->term 2
+-------------------------------+
| 20001 | 5 | 6 |
| 20002 | 6 | 4 |
| 20003 | 7 | 9 |
+-------------------------------+
Tried
(select st-id from result-table where term=1) union (select st-id from result-table where term=2)
but it appends the result.
you should use a join (left if the rows don't always match inner if the always match)
select a.st-id, a.score as `score->term 1`, b.score as `score->term 2`
from `result-table` as a
left join `result-table` as a on a.`st-id` = b.`st-id`
where a.term=1
and b.term=2
or
select a.`st-id`, a.score as `score->term 1`, b.score as `score->term 2`
from `result-table` as a
inner join `result-table a`s a on a.`st-id` = b.`st-id`
where a.term=1
and b.term=2
and you should not use name as st-id (the - is an operatore in mysql) .. if you really need use backtics
i am loosing it over the following problem:
i have a table with participants and points. each participant can have up to 11 point entries of which i only want the sum of the top 6.
in this example lets say we want the top 2 of 3
+----+---------------+--------+
| id | participantid | points |
+----+---------------+--------+
| 1 | 1 | 11 |
+----+---------------+--------+
| 2 | 3 | 1 |
+----+---------------+--------+
| 3 | 3 | 4 |
+----+---------------+--------+
| 4 | 2 | 3 |
+----+---------------+--------+
| 5 | 1 | 5 |
+----+---------------+--------+
| 6 | 2 | 10 |
+----+---------------+--------+
| 7 | 2 | 9 |
+----+---------------+--------+
| 8 | 1 | 3 |
+----+---------------+--------+
| 9 | 3 | 4 |
+----+---------------+--------+
as a result i want something like
+---------------+--------+
| participantid | points |
+---------------+--------+
| 2 | 19 |
+---------------+--------+
| 1 | 16 |
+---------------+--------+
| 3 | 8 |
+---------------+--------+
(it should be ordered DESC by the resulting points)
is this at all possible with mysql? in one query?
oh and the resulting participant ids should be resolved into the real names from another 'partcipant' table where
+----+------+
| id | name |
+----+------+
| 1 | what |
+----+------+
| 2 | ev |
+----+------+
| 3 | er |
+----+------+
but that should be doable with a join at some point... i know...
Using one of the answers from ROW_NUMBER() in MySQL for row counts, and then modifying to get the top.
SELECT ParticipantId, SUM(Points)
FROM
(
SELECT a.participantid, a.points, a.id, count(*) as row_number
FROM scores a
JOIN scores b ON a.participantid = b.participantid AND cast(concat(a.points,'.', a.id) as decimal) <= cast(concat(b.points,'.', b.id) as decimal)
GROUP BY a.participantid, a.points, a.id
) C
WHERE row_number IN (1,2)
GROUP BY ParticipantId
Had an issue with ties until I arbitrarily broke them with the id
I have two tables:
Table a:
+----+------+
| id | data |
+----+------+
| 1 | 450 |
| 2 | 500 |
| 3 | 550 |
| 4 | 600 |
| 5 | 650 |
+----+------+
Table b:
+----+------+------+
| id | a_id | note |
+----+------+------+
| 1 | 2 | 25 |
| 2 | 5 | 10 |
+----+------+------+
I need a query that returns a table that consists of every row from table a with the notes from table b. I want 0 filled in where a note isn't available on a row. I want it to look like this:
+----+------+------+
| id | data | note |
+----+------+------+
| 1 | 450 | 0 |
| 2 | 500 | 25 |
| 3 | 550 | 0 |
| 4 | 600 | 0 |
| 5 | 650 | 10 |
+----+------+------+
How do I do that?
select a.id, a.data, coalesce(b.note, 0) as note
from a
left join b on a.id = b.a_id
What are you looking for is called LEFT/RIGHT JOIN. This question will give you more details about what they are.
Assume you have a query like:
SELECT * FROM a LEFT JOIN b ON some_condition;
Then, its output will contain every row from table a, along with data from table b where the condition is met. For rows where the condition is not met, the columns with data from b will contain null.
Here is problem I have:
Table A
id_a | name
---------------------
1 | name-A
2 | name-B
3 | name-C
4 | name-D
5 | name-E
6 | spcial_type
Table B
id_b | id_a | condition | subtype
-------------------------------------------------
1 | 2 | 1 | 1
2 | 1 | 0 | 1
3 | 1 | 1 | 2
4 | 2 | 0 | 1
5 | 4 | 0 | 1
6 | 5 | 1 | 1
7 | 2 | 1 | 3
Terms:
Table A: “special_type” exluded
Table A: rows not present in Table B included
Table B: all with condition=0 exluded
Result Table:
id_r | id_a | id_b | name | condition
-----------------------------------------------
1 | 1 | 3 | name-A | 1
2 | 2 | 1 | name-B | 1
3 | 5 | 6 | name-E | 1
4 | 2 | 7 | name-B | 1
5 | 3 | null | name-C | null
table A.subtype is only aux. to show that id_a can be stored many times with condition=1
What I tried:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A
LEFT JOIN Table z ON x. id_a = z. id_a
but this got me items with condition=0, which I do not want
so I tried:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A
LEFT JOIN Table z ON x. id_a = z. id_a where z.condition=1
but that idea excluded items from Table A not present in Table B, and I want these items.
Can it be don inside of MySQL, or do I need scripting lang. to sort it out?
Thoughts anyone?
OK
I must have had a temp. black out.
Here it is:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A
LEFT JOIN Table z ON x. id_a = z. id_a AND z.condition=1
condition below
AND z.condition=1
was the key, when placed in join condition not in where clause