B
B
Billy Milligan2015-08-26 11:54:46
JavaScript
Billy Milligan, 2015-08-26 11:54:46

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;

Let's say 5 films are suitable for these parameters.
Is it possible to make a query so as not to take into account, for example, year, etc., in order to get 10 films in the end according to the most appropriate parameters?
Or do you have to make multiple requests?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Z
zendor, 2019-03-05
@Chefranov

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

D
Dmitry, 2015-08-26
@Billy_Milligan

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

The expression itself is meaningless.
The condition is for that and the condition so that we always know whether it was fulfilled or not. Well, Boolean is not easy ...
Although I seem to have deceived you ...
You can solve it without a lot of queries ..
Try it, it seems like what you need
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

T
Talgat Baltasov, 2015-08-26
@talgatbaltasov

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

A
AlikDex, 2015-08-26
@AlikDex

what prevents to use a range of some parameters? For example

`year`<='1998'
OREDER BY `your_params`, `year`

50k records is very little.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question