1

My query selects multiple fields from 4 tables and I need to add OUTER JOIN or LEFT OUTER JOIN to compare values from another table.

SELECT *, table1.name, table2.wage, table3.shift, table4.vacation
FROM table1, table2, table3, table4 OUTER JOIN table5 ON table1.name = table5.position
WHERE table1.name = table2.name
AND table2.wage = table3.wage

This is just an example but how do I make it work, it seems to work if I have just one table in FROM clause.

2 Answers 2

2

Use ANSI syntax for all your joins, eg

SELECT table1.name, table2.wage, table3.shift, table4.vacation,
  table5.someDateTimeField
FROM table1
INNER JOIN table2 ON table1.name = table2.name
INNER JOIN table3 ON table2.wage = table3.wage
INNER JOIN table4 ON something = table4.something -- can't see this one in your example
LEFT JOIN table5 ON table1.name = table5.position
WHERE someArbitraryFilter BETWEEN 3 AND 7;

Also, your SELECT clause looks all wrong. I don't know which * you're after and you're missing commas

Sign up to request clarification or add additional context in comments.

2 Comments

What if i need to select something else from table table5 e.g.date and time how i can make so if i need regular select not with join also one of filters is BETWEEN 3 AND 7 how can i represent it in this query
@BUddhaxx I've updated my answer though I'm not entirely sure what you mean
0

Try using explicit JOIN's instead of those implicit ones:

SELECT table1.name, table2.wage, table3.shift, table4.vacation
FROM table1
JOIN table2 ON table1.name = table2.name
JOIN table3 ON table2.wage = table3.wage
JOIN table4 ON ??
LEFT JOIN table5 ON table1.name = table5.position

plus, it seems you are missing a condition for table4 join.

2 Comments

What if i need to select something else from table table5 e.g.date and time how i can make so if i need regular select not with join
@BUddhaxx - What do you mean not with JOIN? The joins you already have are going to return several rows - would all of them get the same value from table5?

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.