Answer the question
In order to leave comments, you need to log in
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 . ") ");
Answer the question
In order to leave comments, you need to log in
>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.
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 questionAsk a Question
731 491 924 answers to any question