如何在foreach数组中运行此SQL以获取唯一的查询

时间:2016-12-31 09:53:23

标签: php sql arrays

我有一个数组,

$array = array("rating_battery", "rating_display", "rating_design");

数组输出;

Array
(
    [0] => rating_battery
    [1] => rating_display
    [2] => rating_design
)

我有SQL查询,

$query = "SELECT ID, meta_value, meta_key
FROM table_name
WHERE meta_key = $value"

我使用foreach;

foreach ( $array as $value ) {
    $query = "SELECT ID, meta_value, meta_key
    FROM table_name
    WHERE meta_key = $value"
}

如何使用 $query 获得与此类似的 foreach

1. $query_rating_battery with meta_key = 'rating_battery'
2. $query_rating_display with meta_key = 'rating_display'
3. $query_rating_design with meta_key = 'rating_design'

请帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

我认为你可以使用WHERE IN

SELECT ID, meta_value, meta_key
FROM table_name
WHERE meta_key IN implode(',', $value)