I am trying to join two tables from a database; energyImport
and sunAlt
.
energyImport
has three columns: timestamp_id
, energy
, duration
.
sunAlt
has two columns: timestamp_id
, altitude
I am doing an inner join on these two tables using the SQL:
SELECT *
FROM energyImport
INNER JOIN sunAz ON sunAz.timestamp_id = energyImport.timestamp_id;
The output from this is:
timestamp_id,duration,energy,timestamp_id,altitude
1601769600,1800,81310,1601769600,0.0
1601771400,1800,78915,1601771400,0.0
1601773200,1800,78305,1601773200,0.0
The problem is that the timestamp_id
column is repeated. How can I join these columns and only include the first timestamp_id
?
CodePudding user response:
Replace the * with
energyImport.timestamp_id,energyImport.duration, energyImport.energy,
sunAz.altitude
CodePudding user response:
Either you specify the columns that you want in the results:
SELECT e.timestamp_id, e.duration, e.energy, s.altitude
FROM energyImport e INNER JOIN sunAz s
ON s.timestamp_id = e.timestamp_id;
Or, use NATURAL
instead of INNER
join, so that the join is based on the columns(s) with the same names of the 2 tables (if this fits your requirement), because NATURAL
join returns only 1 of each pair of these columns:
SELECT *
FROM energyImport NATURAL JOIN sunAz;
See the demo.