D
D
Dmitry2018-02-26 11:17:02
PHP
Dmitry, 2018-02-26 11:17:02

How to select data from a table based on similar data from their array?

Hello. The point is this. There is an array There is a MySQL database, looks like this: id|name|dat 1 |user1|1 2 |user2|1 6 3 |user3|2 3 4 |user4|5 10 5 |user5|10 12 6 |user6|5 etc. I make a request
$arr={1, 2, 3, 4, 5}

$arr_lists = '\'' . implode ( "','",  $arr ) . '\'';
$req = mysqli_query($db, "SELECT * FROM `table` WHERE `dat` IN(". $arr_lists . ") ");

And I only get data from id 1 and 5.
Tell me how can I make a request so that it searches for similar data, like how the LIKE command works, so that I get id 1, 2, 3, 4, 6

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2018-02-26
@Joker2705

>like how the LIKE command works
If you can't do without LIKE, then compose a query with the number of LIKEs depending on the number of array elements. But, such a sample will work for an awfully long time.

D
Dmitry Kim, 2018-02-26
@kimono

SELECT * FROM `table` WHERE `dat` REGEXP '[12345]'

R
Rsa97, 2018-02-26
@Rsa97

Bring the database to normal form, select the `dat` field in a separate table `user_dats` (`user_id`, `dat`), where each row will contain only one value from `dat` and a pair (`user_id`, `dat` ) forms the primary key.
Then use query

SELECT * 
  FROM (
    SELECT `user_id`
      FROM `user_dats`
      WHERE `dat` IN (1, 2, 3, 4, 5)
      GROUP BY `user_id`
  ) AS `uid`
  JOIN `table` ON `uid`.`user_id` = `table`.`id`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question