如何从 table 获取唯一 ID 数组

how to get unique id array retrieve from table

我有一组来自 table 的数据。它有多个字段,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)。
谁有解决办法或想法?

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

select post_id from wp_postmeta where post_id = $post_id limit 1

假设这是 mysql

试试这个

Select * 来自 wp_postmeta 按 post_id 分组。

这将 return 所有具有唯一 post_id 值的行。然后你可以遍历数组并只打印那些需要的数据。

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.

试试这个

$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);

您可以使用这个查询:

SELECT post_id 来自 wp_postmeta 分组 post_id