1

i have 2 tables: jobs and cods

     jobs                                          cods

idx   descr                                    cod       descr      idx

1    teacher                                   codx      codingx     1
2    programmer                                cody      codingy     4 
3    sailor                                    codz      codingz     2
4    medic                                     codw      codingw     3
5    student                                   cods      codings     1  
                                               codw      codingw     1

i want the select, return the rows with codx and codw where the idx column is the join

id    descr            codx      desc_codx     codw    descr_codw    
1     teacher          codx      codingx       codw     codingw
2     programmer       null       null         null     null     
3     sailor           null       null         codw     codingw
4     medic            null       null         null     null  
5     student          null       null         null     null 

Is it possible with only 1 select?

select x.id, x.descr,a.cod as codx a.desc as desc_codx,b.cod as codw, b.desc as desc_codw 
from jobs x, cods a, cods b 
where x.id= a.id(+) 
  and x.id = b.id(+) 
  and a.cod= 'codx' 
  and b.cod= 'codw'
1
  • What is difficult about this? What have you tried? Commented Nov 26, 2015 at 22:45

2 Answers 2

2

Yes, it is possible. You need two joins to do that so that you can make separate columns for codx and codw.

SELECT
  j.idx, j.descr,
  c.cod AS codx, c.descr AS desc_codx,
  c2.cod AS codw, c2.descr AS desc_codw
FROM
  jobs j
  LEFT JOIN cods c ON j.idx = c.idx AND c.cod = 'codx'
  LEFT JOIN cods c2 ON j.idx = c2.idx AND c2.cod = 'codw'

You could also do that using only 1 JOIN and working with CASE statements. This may seem like more to write, but performance-wise it is far better, as we scan table cods only once and evaluating CASEs pays off much more.

SELECT
  j.idx, j.descr,
  CASE WHEN c.cod = 'codx' THEN c.cod END AS codx,
  CASE WHEN c.cod = 'codx' THEN c.descr END AS desc_codx
  CASE WHEN c.cod = 'codw' THEN c.cod END AS codw,
  CASE WHEN c.cod = 'codw' THEN c.descr END AS desc_codw
FROM
  jobs j
  LEFT JOIN cods c ON j.idx = c.idx AND c.cod IN ('codx', 'codw')
Sign up to request clarification or add additional context in comments.

4 Comments

What exactly do you mean by (+) syntax? Doesn't my answer give you expected results?
I can see your comment. Why would you use (+) instead of x when calling a column named idx? Also, putting conditions between two tables into WHERE implies an INNER JOIN. This is not what you want. Please, try the code from this answer.
as you can see my comment to gordon also i made a select with left join using the plus (+) operator you known? i think its' the same...your select go to try Tomorrow if work it's half past midnight.(in my state). thanks see you asap
I'm not familiar with a plus operator. Also, your comment doesn't contain idx column names, but id. Could you please provide me with a reference to (+) operator you've mentioned?
0
select jobs.* , cods.* from jobs,cods where jobs.idx=cods.idx;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.