N
N
nicklayk2017-11-08 20:33:12
css
nicklayk, 2017-11-08 20:33:12

MySQL How to correctly select a row with an occurrence from a database?

I have two fields in my database.
For example:
Content of field 1: 1, 2, 6
Content of field 2: 1, 3, 6
There is an id.
How can I get rows from the database, in field 1 of which id is included, in field 2 it is not included.
LIKE, NOT LIKE is not suitable, because under them you will have to rebuild the base and write ;1;2;3;, instead of 1, 2, 3

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene Chefranov, 2019-08-15
@Chefranov

Something like this has already been How to make a carousel? just convert to hover

I
Immortal_pony, 2017-11-08
@nicklayk

SELECT *
FROM `table` 
WHERE 1
  AND FIND_IN_SET('1', `field`) > 0
  AND FIND_IN_SET('2', `field`) = 0

Alternative option:
SELECT *
FROM `table` 
WHERE (
    (`field` LIKE '1,%' OR `field` LIKE '%,1,%' OR `field` LIKE '%,1') AND
    (`field` NOT LIKE '2,%' AND `field` NOT LIKE '%,2,%' AND `field` NOT LIKE '%,2') 
)

A
Alexander Taratin, 2017-11-08
@Taraflex

Switch to PostgreSQL which supports array type https://postgrespro.ru/docs/postgrespro/9.6/arrays

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question