There are two MySQL tables:
table1
id | time | status
-----------------------
1 | 10:00 | conn |
1 | 10:01 | disconn |
2 | 10:02 | conn |
2 | 10:03 | disconn |
3 | 10:04 | conn |
table2
id | time |
------------
3 | 10:05 |
If there is no disconn time value for ceratin id then take it from table2.
What is sql query to get wished result:
id | conn | disconn|
--------------------
1 | 10:00| 10:01 |
2 | 10:02| 10:03 |
3 | 10:04| 10:05 |
2
Answers
You can use
LEFT JOIN
andCOALESCE
for this case. Here is sample code:Here is the sample output:
Here is fiddle link
Alternatively, You can also use
GROUP BY
withoutself join
as follows: