Home > Software design >  SQL Select the same row multible times but with diffrent queries
SQL Select the same row multible times but with diffrent queries

Time:06-22

I have 4 tables in my SQL project and I want to use the same row of 1 table with 2 different queries at the moment. I managed to get the result I wanted once but I can't figure out how to get another result with the same column in the same row.

This is my query now:

SELECT currencies.currency, swaps.you_send, swaps.you_send, currencies.currency
FROM currencies, swaps
WHERE swaps.currency_to = currencies.id

Then I get this as result

as you can see in the last row, there is the same row, that should be a different result for the different result this query is needed to get the right result

SELECT currencies.currency, swaps.you_send, swaps.you_get, currencies.currency
FROM currencies, swaps
WHERE swaps.currency_to = currencies.id

Then I get this as result

But what I need is that results the first one in the first row of currency the second in the last one

I tried it with UNION but then they get shown among themselves.

Would be very kind if somebody could help me with this problem.

CodePudding user response:

You select twice the same, thats why you get the same value.

Something like this should work:

SELECT t.currency_from, t.currency_to
FROM Table1 t
JOIN currencies c1 ON c1.name = t.currency_from
JOIN currencies c2 ON c2.name = t.currency_to

I have minifed the example because i dont know your SQL Tables.

Please update your question to make it more clear

CodePudding user response:

Thanks I managed to do it with the inspiration of your snippet.

SELECT s.you_send,c1.currency, s.you_get, c2.currency
FROM swaps s
JOIN currencies c1 ON c1.id = s.currency_from
JOIN currencies c2 ON c2.id = s.currency_to

    


  • Related