如何从表中获取唯一的id数组

时间:2015-09-23 06:52:01

标签: php phpmyadmin

我有来自表的数据数组。它有多个字段,post_id(int)是其中之一。我想获得返回数组的post_id字段的唯一值。

$data返回查询

foreach ($data as $key => $get_values) {    
         $post_id = $get_values->post_id;

         $get_query = "select DISTINCT (post_id) from wp_postmeta where post_id = $post_id";

         $get_postId = mysqli_query($get_query);<br>
}  

我只想获得DISTINCT(post_id)。
任何人都有解决方案或任何想法?

5 个答案:

答案 0 :(得分:0)

即使只返回一行,结果也将作为数组返回。如果你不关心你想要哪一行(假设有多个匹配),你可以使用类似的东西:

select post_id from wp_postmeta where post_id = $post_id limit 1

假设这是mysql

答案 1 :(得分:0)

试试这个

通过post_id从wp_postmeta组中选择*。

这将返回具有唯一post_id值的所有行。然后,您可以遍历数组并仅打印所需的数据。

答案 2 :(得分:0)

foreach ($data as $key => $get_values) {    
     $post_id = $get_values->post_id;

     $get_query = "select DISTINCT (post_id) from wp_postmeta where post_id = $post_id group by post_id";

     $get_postId = mysqli_query($get_query);<br>
}  

只需添加group by

答案 3 :(得分:0)

试试这个

$post_id ="";
foreach ($data as $key => $get_values) {    
$post_id. = $get_values->post_id.",";
}
$post_id =substr($post_id,0,-1);
$get_query = "select DISTINCT (post_id) from wp_postmeta where post_id in ($post_id)";

$get_postId = mysqli_query($get_query);

答案 4 :(得分:0)

您可以使用此查询:

SELECT post_id FROM wp_postmeta GROUP BY post_id