开发者

mySQL query -- if value in string

I have a string of IDs separated with comma

$myIDs = 22,23,45,895;

How do I write a query to return records for values that correspo开发者_开发技巧nd to the IDs in my string? This does not seem to be right:

SELECT *
FROM t1
WHERE itemID IN ($myIDs)

I guess I'm trying PHP array function here, hah? Is there something like this in mySQL?

Appreciate any suggestions. Thanks.


I think you're missing quotes, ie, the exact query should look like this before evaluation

SELECT *
FROM t1
WHERE itemID IN ('22','23','45','895');

Hence all you've got to do to fix this is:-

$myIDs = array(22,23,45,895);
$myIDs_string = "'".implode("','",$myIDs)."'";

then in whatever PHP/SQL library/framework you select, use PHP to execute the following php query:-

SELECT *
FROM t1
WHERE itemID IN ($myIDs_string);

Hope this helps.


$IDs = array(1,2,3,4,5);
// alternatively, you can write it like this...
// $IDs = "1,2,3,4,5";

if(is_array($IDs))
    $IDs = implode(",",$IDs);

$query = "SELECT * FROM t1 WHERE itemID IN ($IDs)";
echo $query;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜