I
I
Ivan Stroykin2017-01-23 12:15:50
SQL
Ivan Stroykin, 2017-01-23 12:15:50

How to correctly execute a condition in a condition?

Good day,
I'm sitting and really stupid on a small problem with a condition. The table is from my head but matches the real one.
| date | id | test1 | test2 | number |
In the query, we must get the entire row, but under the following conditions:
1) "our date" >= table.date
2) if the id in the query is in the table ("our_id" = table.id), then we take the row
3) if there is such an id no, we look at the match on "our_test1" = table.test1 AND "our_test2" = table.test2
Now I registered only the option that, with an existing id, still finds it by the second condition

SELECT *
FROM table
WHERE
    CASE WHEN id = "наш_id" THEN TRUE
    ELSE
        CASE WHEN test1 = "наш_test1" AND test2 = "наш_test2" THEN TRUE END
    END
ORDER BY date DESC
LIMIT 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2017-01-23
@StivinKing

Actually, directly from the verbal description:
where
table.date <= our date
or
table.id = our_id
or
("our_test1" = table.test1 AND "our_test2" = table.test2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question