Answer the question
In order to leave comments, you need to log in
How to make a request with an optional condition?
For example, there is a table tFilm with fields name, country, genre, year.
How to make a request so that you get 10 films.
SELECT * FROM `tFilm` WHERE `country`='34' AND `genre` = '4' AND `year` = '1998' LIMIT 10;
Answer the question
In order to leave comments, you need to log in
Because in an object literal you are directly specifying the name of the property (the string 'name' in this case). Do this:
Computed property names
Technically feasible, but the load on the server is not justified.
Temporary tables + a bunch of Joins. It's not worth it
. In general, it is not necessary to fulfill the condition:
Where
(nekoe_uslovie) or 1=1
select
case country when '34' then 1 else 0 end as sort1,
case genre when '4' then 1 else 0 end as sort2,
case year when '1998' then 1 else 0 end as sort3,
country,
genre,
year
from
'tfilm'
sort by sort1, sort2, sort3
limit 10
Well, for anyone, you need several queries, since at the beginning we don’t know how many such records, if less then in the next. remove the year in the request, if it is less than 10, then remove something else
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question