我如何在MySQL中写这个?

时间:2012-11-19 00:47:46

标签: php mysql arrays

我想搜索MySQL数据库,将列列表与数组进行比较。我怎么能这样做,可能都在MySQL内?

这就是我想用PHP做的事情:

<?php
$array = array("a", "b", "c", "d");
// 'c' doesn't exist in the database
$result = $this->mysqli->query("Query");
// Result should be array("a", "b", "d")
?>

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用the IN clause执行此操作。

SELECT * FROM <table> WHERE <col> IN <array>

答案 1 :(得分:0)

试试这个:

// The array
$array = array("a", "b", "c", "d");

// This takes the array and puts it into a string like "a,b,c,d"
$array_for_query = implode(',', $array);

$result = $this->mysqli->query("SELECT * FROM table WHERE the_letter IN ({$array_for_query})");

分别用您的表名和列名替换tablethe_letter