I'm in the sql is not very. Help, please understand and build the sample correctly. There is a table with many fields. From this table you only need to select fields with a minimum value of the type like_1 BETWEEN 1 AND 4 , and leave cells that are not in your range empty.

 "SELECT `clerk_id`, `like_1`, `like_2`, `like_3`, `like_4`, `like_5`, `like_6`, `like_7`, `like_8`, `like_9`, `like_10`, `like_11`, `like_12`, `like_13`, `like_14`, `like_15`, `like_16`, `like_17`, `like_18`, `like_19`, `like_20`, `like_21` FROM ".$selected_operator." WHERE like_1 BETWEEN 1 AND 3 OR like_2 = 3 OR like_3 = 2 OR like_4 BETWEEN 7 AND 10 OR like_5 BETWEEN 4 AND 5 OR like_6 BETWEEN 10 AND 15 OR like_7 BETWEEN 4 AND 5 OR like_8 BETWEEN 4 AND 5 OR like_9 BETWEEN 7 AND 10 OR like_10 BETWEEN 5 AND 10 OR like_11 BETWEEN 4 AND 5 OR like_12 BETWEEN 4 AND 5 OR like_13 = 2 OR like_14 BETWEEN 4 AND 5 OR like_15 BETWEEN 4 AND 5 OR like_16 BETWEEN 4 AND 5 OR like_17 BETWEEN 4 AND 5 OR like_18 = 0;"; 

That's what I put on, but there are ways to do it normally. I will be glad to any advice or source of information where I can see it) Thanks in advance

  • four
    In SQL, there is only one way to do this normally. It is necessary to alter the table so that it corresponds to the first normal form ( ru.wikipedia.org/wiki/… ) i.e. split it into 2 tables. in the first one there will be only clerk_id and maybe some other single fields describing it. The second table will contain the fields clerk_id, like_num, value. Accordingly, the data are put in separate lines. and then the query will be select clerk_id from tab2 where value BETWEEN 4 AND 5 - Mike
  • nevertheless, I will hope that this is not the only way - Glechik
  • The SQL language was designed to work with normalized data. Step to the left, step to the right, the work is not that not possible, but wildly inconvenient and loads the system wildly. - Mike
  • I understand you, thanks for the help. The question can be considered closed - Glechik

1 answer 1

The answer to the question was given by @Mike. He is faithful. Not built correctly DB. Thanks for the help, I will reconsider a structure.

In SQL, there is only one way to do this normally. It is necessary to alter the table so that it corresponds to the first normal form (ru.wikipedia.org/wiki/…) i.e. split it into 2 tables. in the first one there will be only clerk_id and maybe some other single fields describing it. The second table will contain the fields clerk_id, like_num, value. Accordingly, the data are put in separate lines. and then the query will select clerk_id from tab2 where value BETWEEN 4 AND 5