我试图在互联网上找到答案,我发现了一些东西,但我没有得到它: - )
我只有两张桌子 - wp_users& wp_usermeta。这两个表具有相同的ID(wp_users.ID和wp_usermeta.user_id)。我想要的只是从wp_users获取电子邮件地址,并使用来自wp_usermeta的昵称加入。
我是sql的初学者,这将非常有用,如果有人会解释:-)非常感谢你: - )
<?php
//I just want tu print in echo all user emails connected with their nicknames from two separate tables - wp_users
//and wp_user_meta. I am trying to connect these tables with "ID" which has these two tables the same (inner join part)
$teacher_table = "SELECT wp_users.user_email, wp_usermeta.nickname
FROM wp_users,wp_usermeta
INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id";
$results_table_main = $wpdb->get_results($teacher_table);
foreach ($teacher_table as $value){
echo $value;
}
?>
答案 0 :(得分:1)
之前的答案和评论将有助于解决此问题,但此处的主要问题是您要查询不存在的列wp_usermeta.nickname
这是你需要做的:
$teacher_table = "SELECT wp_users.user_email, wp_usermeta.meta_value
FROM wp_users INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = 'nickname'";
为了更好地衡量代码的其余部分,请注意返回的变量是一个对象,因此必须这样访问。
$results_table_main = $wpdb->get_results($teacher_table);
foreach ($results_table_main as $value){
echo $value->user_email;
echo $value->meta_value;
}
答案 1 :(得分:0)
您正在使用错误的查询。
global $wpdb;
$sql = "SELECT u.user_email ,
um.meta_value AS nickname
FROM wp_users u
INNER JOIN wp_usermeta um ON u.ID = um.user_id
WHERE um.meta_key = 'nickname'";
$results = $wpdb->get_results($sql);
foreach ($results as $value){
echo "EMAIL : ".$value->user_email . "<br/>";
echo "NICKNAME : ".$value->nickname. "<br/>";
echo '<br/>';
}
我认为这会有所帮助