簡體   English   中英

如何從表中獲取唯一的ID數組

[英]how to get unique id array retrieve from 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

嘗試這個

通過post_id從wp_postmeta組中選擇*。

這將返回所有具有唯一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);

您可以使用以下查詢:

從wp_postmeta GROUP BY post_id中選擇post_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM