我想知道是否有一种合并这两个数据库查询的方法,以便有一个:
$result = $wpdb->get_row("SELECT meta_value FROM ".$wpdb->prefix.
"postmeta WHERE meta_key = '_cat_num' AND post_id = $var");
$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE.
" WHERE id = " . (int)$result->meta_value);
return $name->name;
第一个查询找到类别值,然后用于在AH_FEED_DETAILS_TABLE表中查找名称字段
答案 0 :(得分:2)
您可以通过使用带子查询的IN来让数据库为此完成工作,如下所示:
$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE.
" WHERE id IN (SELECT meta_value FROM ".$wpdb->prefix.
"postmeta WHERE meta_key = '_cat_num' AND post_id = $var)");
答案 1 :(得分:0)
$result = $wpdb->get_row("SELECT name FROM ".$wpdb->prefix
.AH_FEED_DETAILS_TABLE." AS cat_tbl JOIN postmeta ON
cat_tbl.id=postmeta.meta_value
WHERE postmeta.meta_key = '_cat_num' AND postmeta.post_id = $var");
return $result->name;
我希望这有助于提出这个想法