Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL left self-join with WHERE clause dependencies between the two copies of the table

Tags:

sql

outer-join

The following two sentences:

hello there
bye!

are represented in the table sentence_words by:

WORD_ID  SENTENCE_ID    WORD    WORD_NUMBER
10       1              hello   1
11       1              there   2
12       2              bye!    1

I want to do an outer join query that gives me the results:

WORD1      WORD2
hello      there
bye!       NULL

Note that I may want to start in the middle of the sentence so I cannot assume that word2 has word_number = 2. If I choose my_start_number = 2 then the query should give me:

WORD1   WORD2
there   NULL

I tried:

(my_start_number = 1)

select  s1.word word1, s2.word word2
from sentence_words s1
left join sentence_words s2
on s1.sentence_id = s2.sentence_id
where s1.word_number = my_start_number
 and (s2.word_number = s1.word_number +1 or s2.word_number is null);

That only gives me a result if there are two words in the sentence. I'm not sure what to do that isn't way complicated.

like image 224
user984003 Avatar asked Apr 26 '12 13:04

user984003


People also ask

How do you use self join with left join in SQL?

To perform a SELF JOIN in SQL, the LEFT or INNER JOIN is usually used. SELECT column_names FROM Table1 t1 [INNER | LEFT] JOIN Table1 t2 ON join_predicate; Note: t1 and t2 are different table aliases for the same table. You can also create the SELF JOIN with the help of the WHERE clause.

Can I use WHERE clause in join?

To use the WHERE clause to perform the same join as you perform using the INNER JOIN syntax, enter both the join condition and the additional selection condition in the WHERE clause. The tables to be joined are listed in the FROM clause, separated by commas. This query returns the same output as the previous example.

Is left join with WHERE clause same as inner join?

You'll use INNER JOIN when you want to return only records having pair on both sides, and you'll use LEFT JOIN when you need all records from the “left” table, no matter if they have pair in the “right” table or not.

Can we use WHERE clause in left outer join?

In the WHERE clause, you can specify left and right outer joins only. To outer join tables TABLE1 and TABLE2 and return non-matching rows from TABLE1 (a left outer join), specify TABLE1 LEFT OUTER JOIN TABLE2 in the FROM clause or apply the (+) operator to all joining columns from TABLE2 in the WHERE clause.


1 Answers

Move the word_number + 1 requirement into the LEFT JOIN.

SELECT
  s1.word word1, s2.word word2
FROM
  sentence_words s1
LEFT JOIN
  sentence_words s2
    ON  s2.sentence_id = s1.sentence_id
    AND s2.word_number = s1.word_number + 1
WHERE
  s1.word_number = my_start_number

NECRO EDIT:

Although the above fixes the use of LEFT JOIN, I would suggest not using joins at all...

SELECT
  sentence_id,
  MAX(CASE WHEN pos = 0 THEN word END)   AS word1,
  MAX(CASE WHEN pos = 1 THEN word END)   AS word2
FROM
(
  SELECT
    sentence_id,
    word_number - MY_START_NUMBER   AS pos,
    word
  FROM
    sentence_words
)
  AS offset_sentence_words
WHERE
  pos IN (0, 1)
GROUP BY
  sentence_id
like image 126
MatBailie Avatar answered Nov 15 '22 18:11

MatBailie