SELECT zmogaus_id, hobio_id, hobio_pav2
FROM zmogaus_hobiai
LEFT JOIN hobiai
ON zmogaus_hobiai.hobio_pav2=hobiai.hobio_pav
WHERE zmogaus_id=3
Using this I want to get
zomgaus_id =3 . hobio_id =1 . hobio_pav2 =name1 (from hobio_pav)
zomgaus_id =3 . hobio_id =2 . hobio_pav2 =name2 (from hobio_pav)
But hobio_pav2 stays as an empty column. I tried not having hobio_pav2 column but the problem persists of not adding hobio_pav values to corresponding IDs to hobio_id.
hobio_pav2 column is empty
EDIT:
sample data:
table hobiai: id = 1, hobio_pav = name1;id = 2, hobio_pav = name2;id = 3, hobio_pav = name3;
table zmogaus_hobiai:
id =1, zmogaus_id=2, hobio_id=2, hobio_pav2 = null;
id =2, zmogaus_id=2, hobio_id=3, hobio_pav2 = null;
id =4, zmogaus_id=3, hobio_id=1, hobio_pav2 = null;
id =5, zmogaus_id=3, hobio_id=3, hobio_pav2 = null;
Desired result:
generated table:
zmogaus_id=3, hobio_id=1, hobio_pav2=name1;
zmogaus_id=3, hobio_id=3, hobio_pav2=name3;
Got result:
generated table:
zmogaus_id=3, hobio_id=1, hobio_pav2= ;
zmogaus_id=3, hobio_id=3, hobio_pav2= ;
3
Answers
Are you looking for
coalesce()
?If you want to update
zmogaus_hobiai
And your SELECT would look like this
From your sample data, it looks like you just need to fix your join condition – you should be joining on
id
s rather than on those other string columns:Note that I added table aliases and prefixed all columns with the table they belong to. This makes the query easier to read, and avoids ambiguity when different tables have homonym columns.