1

I have this sql query:

...
LEFT JOIN users ON
users.id = mod.id and mod.level = 1
...

But if don't found any result with mod.level = 1, i wish search with mod.data > 1 (users.id = mod.id and mod.data > 1)

2
  • If you need either mod.level = 1 or mod.data > 1, use LEFT JOIN users ON users.id = mod.id WHERE mod.level = 1 OR mod.data > 1 Commented Sep 9, 2010 at 13:52
  • 1
    @cypher: But that would include the mod.data > 1 rows even if there exists some mod.level = 1 rows. Commented Sep 9, 2010 at 14:00

5 Answers 5

1

You can additionally filter on the JOIN like this:

LEFT JOIN users ON users.id = mod.id AND (mod.level = 1 OR mod.data > 1)
Sign up to request clarification or add additional context in comments.

1 Comment

This is more easily written modl.level >= 1, but this will return rows for both mod.level = 1 and mod.data > 1. It seems the OP only want rows where mod.data > 1 if there are no rows where mod.level = 1.
1

maybe using a XOR?

...
LEFT JOIN users
       ON users.id = mod.id
    WHERE mod.level = 1
      XOR mod.data > 1
...

this will get rows where mod.level is 1 or mod.data is greater than 1, but not rows where level is 1 and data is greater 1 at the same time

if you only want to look at mod.data when mod.level is not 1 use the following condition:

...
WHERE mod.level = 1
   OR (mod.level != 1
  AND mod.data > 1)
...

1 Comment

I don't think this works the way you expect. The XOR will not examine multiple rows at once, which I think is what the OP needs. Your second example is more simply written as where mod.level >= 1.
1

You can switch your and to a WHERE and use an OR function:

LEFT JOIN users ON
users.id = mod.id 
WHERE mod.level = 1
OR mod.data > 1

1 Comment

This is more easily written where modl.level >= 1, but this will return rows for both mod.level = 1 and mod.data > 1. It seems the OP only want rows where mod.data > 1 if there are no rows where mod.level = 1.
0

Try something like this:

select *
from MyTable m
left outer join (
    select id
    from MyTable
    where level = 1
) ml on m.id = ml.id
left outer join users u on m.id = u.id
    and (u.id = m1.id or (m1.id is null and m.level > 1))

Comments

0

It may be slow but the join returns all rows that fit either case. Then uses the where clause to filter out the rows you don't want.

Select *
From 
  LEFT JOIN users ON users.id = mod.id AND (mod.level = 1 OR mod.data > 1) 
Where
  Case
    When mod.level = 1 then 1 
    When Not Exists(Select 1 from users Where users.id = mod.id and mod.level=1) 
      AND mod.data > 1 then 1
    Else 0 END = 1;

1 Comment

You are missing a table name after From. Additionally, AND (mod.level = 1 OR mod.data > 1) is more simply written as AND mod.level >= 1.

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.