1

Hello I am currently trying to combine, 3 types of tables. I have mange to do it with 2 that have same values but what happends when you want the a 3rd table that has same pk as second but not first? And I hit a giant brick wall.... how do I solve this issue?

SELECT xxx.orderid, xxx.ordertime,xxx2.produdct, xxx2.product type, xxx3.priscut xx3.pricename FROM xxx
INNER JOIN xxx2 ON xxx.orderid= xxx2.orderid
RIGHT JOIN xxx3 ON xxx2.productid = xx3.productid;

My Best Wishes, Mike

MikeBroski
  • 64
  • 1
  • 8
  • 1
    I'm not exactly sure why you would use a `right join` (try an `inner join` or a `left join` depending on what you want), but your code would work - why would you doubt that? You can join as many tables as you want on any condition you want, they don't have to be directly linked to the first table (and are in fact most of the time not). – Solarflare Jun 04 '16 at 17:34
  • well whenever it says I have an error, on the last line... why is when I try to right join the previous join.. :( – MikeBroski Jun 04 '16 at 17:36
  • 1
    So standard question: What do you get and what do expect? (And add a comma after `priscut` in the first line and of course `xxx3` in the last join instead of `xx3` - but i guess these are just placeholders anyway). Do you know what a `right join` is? Try a normal `join` (without the `right`) first, I guess that is what you are expecting. – Solarflare Jun 04 '16 at 17:37
  • YES I mange to fix it myself, what I wanted was basically to have a select statement that gives me values from 3 tables where 2 had same ok and one had same pk as the one I wanted to join with. What I did was 2 RIGHT JOIN and it seems to work.. but from my understand does that mean I will get NULL in the one I wanted to join? – MikeBroski Jun 04 '16 at 17:43

1 Answers1

1

Simply use inner join

SELECT 
 xxx.orderid
, xxx.ordertime
,xxx2.produdct
, xxx2.producttype
, xxx3.priscut xx3.pricename 
FROM xxx
INNER JOIN xxx2 ON xxx.orderid= xxx2.orderid
INNER JOIN xxx3 ON xxx2.productid = xx3.productid;
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thanks, you are absolutly right inner join is the right case here to avoid NULL values I think.. a little confused why ever to use a right and left join... – MikeBroski Jun 04 '16 at 17:46
  • 1
    Yes for avoid null value inner join is the right solution because in based only resulting rows tha match .. (for each inner join) – ScaisEdge Jun 04 '16 at 17:48