0

I have two tables and I would like to generate a query relating the tables. Until now I have this query:

SELECT Table1.*, table2.* FROM Table1 INNER JOIN table2 ON table2.FK_ID = Table1.ID

but I return the 3 rows repeating the data in table 1.

I would like my query to return the data as follows (see image)

enter image description here

Any suggestions to see if it is possible to make my requirement?

Thank you!

===============================

UPDATE

I Tried with this query

SELECT  Table1.*, 
    (SELECT Table2.Columna2 WHERE Table2.Columna4='ALTA') AS Name1,
    (SELECT Table2.Columna2 WHERE Table2.Columna4='ATENCION') AS Name2
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.FK_ID
GROUP BY Table1.ID

but in my Name2 always is NULL

2
  • You can look at pivot tables that will show you how to do this, however its probably better to handle the rows in your application language.
    – danblack
    Feb 17, 2020 at 23:19
  • can you provide the data as text and/or make a dbfiddle example.
    – nbk
    Feb 18, 2020 at 0:00

1 Answer 1

0
SELECT  Table1.*, 
    (SELECT Table2.Columna2 WHERE Table2.Columna4='ALTA') AS Name1,
    (SELECT Table2.Columna2 WHERE Table2.Columna4='ATENCION') AS Name2
    (SELECT Table2.Columna2 WHERE Table2.Columna4='CLIENTE') AS Name3
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.FK_ID

is what you want.

But there are other ways to achieve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.