I have the following setup:
An array in php like this:
$arr = array(1, 2, 3, 4);
A query that looks like this:
SELECT `epicThing` FROM `epicTable` AS `epic`
WHERE
SELECT `ids` FROM `someTable`
WHERE `epic`.`somethingSomething` = `someTable`.`somethingElse`
(<<<< HERE IS MY PROBLEM >>>>)
The subquery returns something like (1, 2, 3, 4, 5, 6, 7, 8, 9).
Now what I need to check is that each of the elements from the array is in that returned answer of the subquery.
Basically something like
SELECT `epicThing` FROM `epicTable` AS `epic`
WHERE
'1' IN (
SELECT `ids` FROM `someTable`
WHERE `epic`.`somethingSomething` = `someTable`.`somethingElse`
)
AND
'2' IN (
SELECT `ids` FROM `someTable`
WHERE `epic`.`somethingSomething` = `someTable`.`somethingElse`
)
AND
'3' IN (
SELECT `ids` FROM `someTable`
WHERE `epic`.`somethingSomething` = `someTable`.`somethingElse`
).......
But for each element.
For simplicity let's assume that elements are always in order (because I will probably need to convert the array to string), if is not possible otherwise. But I would prefer a general solution if available.
What I DON'T want to do is to get data in php and check it there (this is only a really really small part of a huge query).
If I'm understanding your question correctly, I think this would be the easiest way for you to test that all IDs in your array match to a record in the database:
<?php
$myArray = array(1,2,3);
$myArrayCount = count($myArray);
// Convert array for the query
$queryArray = implode(",", $myArray);
SELECT COUNT(DISTINCT(YourTable.id)) AS count
FROM YourTable
WHERE YourTable.id IN ($queryArray)
HAVING count = $myArrayCount;
?>
Mysql will return empty results if the HAVING count = $myArrayCount; does not match the number of IDs you are checking for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.