2

I was expecting the following query to return null rows where there wasn't a matching row in the WEBSITE_URL table when using the left join. So what i want to see is all url types from the URLTYPE table and any matching records in the WEBSITE_URL table.

SELECT
    WU.NC_WEBSITE_ID_FK,
    WU.TC_URL,
    UT.TC_URLTYPE_NAME
  FROM
    WEBSITE_URL WU,
    URLTYPE UT
  WHERE
    WU.NC_URL_TYPE_ID_FK(+) = UT.NC_URLTYPE_ID
  AND
    NC_WEBSITE_ID_FK = '1622'

This is the output:

1622    http://www.test.com                 PRODUCTION (PUBLIC)

1622    http://preprod.test.com             PREPRODUCTION

1622    http://review.test.com              REVIEW
1
  • select * from WEBSITE_URL where NC_WEBSITE_ID_FK = '1622' and NC_URL_TYPE_ID_FK not in (select NC_URLTYPE_ID from URLTYPE) Commented May 22, 2015 at 11:02

3 Answers 3

1

Try this:

SELECT
    WU.NC_WEBSITE_ID_FK,
    WU.TC_URL,
    UT.TC_URLTYPE_NAME
  FROM
    URLTYPE UT
  LEFT JOIN 
    WEBSITE_URL WU ON WU.NC_URL_TYPE_ID_FK = UT.NC_URLTYPE_ID
               AND WU.NC_WEBSITE_ID_FK = '1622'
Sign up to request clarification or add additional context in comments.

Comments

1

Use a standard join type (outer or left in this case) rather than just in the old sql syntax

SELECT WU.NC_WEBSITE_ID_FK, WU.TC_URL, UT.TC_URLTYPE_NAME
FROM URLTYPE UT
FULL OUTER JOIN WEBSITE_URL WU ON UT.NC_URLTYPE_ID = WU.NC_URL_TYPE_ID_FK(+)
WHERE NC_WEBSITE_ID_FK = '1622'

1 Comment

Please add some context instead of just spitting up some code.
0

First, learn to use explicit join syntax. Few people really understand the old Oracle syntax anymore.

Second, use table aliases on all the columns.

If NC_WEBSITE_ID_FK comes from WEBSITE_URL, then the condition needs to be moved to the ON clause:

SELECT WU.NC_WEBSITE_ID_FK, WU.TC_URL, UT.TC_URLTYPE_NAME
FROM URLTYPE LEFT JOIN
     WEBSITE_URL WU
     ON WU.NC_URL_TYPE_ID_FK = UT.NC_URLTYPE_ID AND
        NC_WEBSITE_ID_FK = '1622';

Also, if NC_WEBSITE_ID_FK is a number, don't put the constant value in single quotes. It is misleading both to people and to the optimizer to mix data types.

2 Comments

This is close to the output that I am after, the only thing that its not doing is outputting the NC_WEBSITE_ID_FK in the null rows, which i'm not sure is possible.
@JeepersCreepers . . . If there is no match, how would it have a value for that column?

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.