S
S
Sergey Sulakov2015-12-30 09:34:57
MySQL
Sergey Sulakov, 2015-12-30 09:34:57

Select from MySQL table by same field values. How is it better?

There is a table of students:
id|name|addr|langs|times|ages
1|Ivan|Kiev;Moscow|rus;ukr|Mon;Fri|4
2|Peter|Kharkov;Moscow|rus|Fri|4
3|Oleg|Riga |rus;lit|mon;tue|5
.......
etc.
From this data, you need to assemble a group - i.e. choose from the database of students with:
1. One city (example: Moscow - Ivan and Peter are suitable)
2. One language (example: Russian - all are suitable)
3. One day of the week (similarly)
4. One age.
One moment: If there is a "selection" in Moscow, for example, then those who have the city of Moscow + can "have" other cities. Also for other fields
. What is the best way to proceed here? And then only idiotic decisions come to mind

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2015-12-30
@Rsa97

In this form, not really. First, normalize the table, put the cities and languages ​​into related tables, convert the days of the week from a text string to the SET data type. Then the search will be simple and clear.

M
Marat, 2015-12-30
@Joysi75

Fully agree with Rsa97. But if you can not change the structure of the database and normalize it, then look for the occurrence of a substring in a string:
1)

select * from table where instr ( concat(';',addr,';'),';Москва;') > 0

2)
select * from table where instr ( concat(';',lang,';'),';рус;') > 0

3)
select * from table where instr ( concat(';',times,';'),';пт;') > 0

etc. ';' surrounded to exclude, for example, New York instead of York, etc.
But all this is terribly (even madly ) wrong in structure and greatly increases the chance of storing erroneous data, etc.
1) Rename the fields (addr -> city, times -> dayofweek, etc.).
2) Create lookup tables and make a foreign key on them
3) Use the correct row type (it's very bad to store everything in text form, especially limited sets.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question