Home > front end >  Adding another column to select output SQL
Adding another column to select output SQL

Time:05-09

I wrote this

SELECT BrojRacuna FROM Racun LEFT OUTER JOIN Stavka ON IDRacun=RacunID, SELECT UkupnaCijena AS 'Ukupna cijena' FROM Stavka;

BrojRacuna means bill number, Racun means bill and UkupnaCijena means price total. Basically I want to add column which outputs price total and is called Ukupna cijena in the same query as the first SELECT LEFT OUTER JOIN. What is the way to do this? Thanks in advance

CodePudding user response:

just do

select 
  Racun.BrojRacuna,
  Stavka.UkupnaCijena AS 'Ukupna cijena'

FROM Racun inner join Stavka 
ON racun.RacunID= stavka.RacunID

in order to do the join right you need to join a a certain and similar column to both tables. like I did on RacounID (I dont know if both your tables have that column)

CodePudding user response:

Some guessing:

SELECT BrojRacuna, UkupnaCijena AS Ukupna_cijena 
FROM Racun 
LEFT OUTER JOIN Stavka 
    ON IDRacun=RacunID

You should add aliases to your tables and qualify the columns with that:

SELECT r.BrojRacuna, s.UkupnaCijena AS Ukupna_cijena 
FROM Racun AS r 
LEFT OUTER JOIN Stavka as s 
    ON r.IDRacun=s.RacunID

It makes it a whole lot easier to understand the query for the next person

  •  Tags:  
  • sql
  • Related