通过id wordpress

时间:2017-03-31 18:38:09

标签: php mysql wordpress

问题

我有两个不同的插件表,我想通过sql获取它们的值。问题是id,我无法找到可以检索数据的连接。

表1(bdt_mappress_posts)

postid = 2和mapid = 3

表2(bdt_mappress_maps)

mapid = 3和obj = json

<? $postid = get_the_ID(); 
$results = $GLOBALS['wpdb']->get_results( 'SELECT obj FROM bdt_mappress_maps INNER JOIN bdt_mappress_posts WHERE bdt_mappress_maps.mapid = bdt_mappress_posts AND   bdt_mappress_posts.postid ='.$postid); print_r($results);
 ?>

$ postid返回我的帖子ID。 sql返回空值,我知道sql但不是那么强大。如果有人可以帮助我或给我相关的帖子,我可以找到答案。先谢谢!

2 个答案:

答案 0 :(得分:0)

假设两个表都与mapid相关,那么你应该使用条件

的连接
$results = $GLOBALS['wpdb']->get_results( 'SELECT mapid 
           FROM bdt_mappress_maps 
           INNER JOIN bdt_mappress_posts 
              on (bdt_mappress_maps.mapid = bdt_mappress_posts
                   AND   bdt_mappress_posts.id ='.$postid));

答案 1 :(得分:0)

<? $postid = get_the_ID(); 
$results = $GLOBALS['wpdb']->get_results( 'SELECT obj FROM bdt_mappress_posts INNER JOIN bdt_mappress_maps WHERE bdt_mappress_posts.mapid = bdt_mappress_maps.mapid AND   bdt_mappress_posts.postid ='.$postid); print_r($results);
?>

解决问题

那应该只改变左sql规则的位置