I want to join two tables with different fields in mysql. I have the following data in the tables:
AQuenteSan | AFriaSan | Realizacao |
---|---|---|
0.58 | 0.61 | 2023-11-14 |
0.52 | 0.64 | 2023-11-15 |
dioxcloro | Realizacao |
---|---|
0.02 | 2023-11-14 |
0.10 | 2023-11-15 |
Now I’m trying to join the data like this:
WITH
exp1 AS (SELECT AQuenteSan, AFriaSan, Realizacao FROM raddb.Reg_Residual WHERE Realizacao BETWEEN '2023-11-14' AND '2023-11-15'),
exp2 AS (SELECT dioxcloro, Realizacao AS Realizacao1 FROM raddb.Reg_Dioxido WHERE Realizacao BETWEEN '2023-11-14' AND '2023-11-15')
SELECT DISTINCT * FROM exp1 JOIN exp2;
And this is the result I got when executing the query shown above:
AQuenteSan | AFriaSan | Realizacao | dioxcloro | Realizacao1 |
---|---|---|---|---|
0.58 | 0.61 | 2023-11-14 | 0.10 | 2023-11-15 |
0.58 | 0.61 | 2023-11-14 | 0.02 | 2023-11-14 |
0.52 | 0.64 | 2023-11-15 | 0.10 | 2023-11-15 |
0.52 | 0.64 | 2023-11-15 | 0.02 | 2023-11-14 |
But the result I want is just this:
AQuenteSan | AFriaSan | Realizacao | dioxcloro | Realizacao1 |
---|---|---|---|---|
0.58 | 0.61 | 2023-11-14 | 0.02 | 2023-11-14 |
0.52 | 0.64 | 2023-11-15 | 0.10 | 2023-11-15 |
How can I get this result?
2
Answers
You need a column to join them on. In your case, you could perform a
LEFT JOIN
:This will give you the result you want. The
LEFT JOIN
makes sure, you only get one instance of an entry in the first table.