我为我的wp网站创建了一个简单的脚本, 我试图从2表的wp数据库中获取正确的数据,这里是代码,
当我显示列“user_ref_id”的结果重复多次时, 这是代码
global $wpdb;
$tableuser=$wpdb->prefix.'user_shah';
$tablecopn=$wpdb->prefix.'user_coupons_shah';
$results_count = $wpdb->get_results("SELECT User_Status, Coupon_Status, User_ref_id FROM $tableuser t1 INNER JOIN $tablecopn t2 ON t1.User_Status=t2.Coupon_Status WHERE t1.User_Status='1'");
$res_countus = count($results_count);
if($res_countus>0)
{
echo "<p><select name='autoref' id='myAutoref' onchange='myRefauto()'>
<option>Select Reference</option>";
foreach($results_count as $results2)
{ echo "<option value='{$results2->User_ref_id}'>{$results2->User_ref_id}</option>";
}
echo "</select></p>";
}
答案 0 :(得分:0)
您需要在您的mysql查询中添加GROUP BY子句。
$results_count = $wpdb->get_results("
SELECT User_Status, Coupon_Status,User_ref_id
FROM $tableuser t1 INNER JOIN $tablecopn t2
ON t1.User_Status=t2.Coupon_Status
WHERE t1.User_Status='1'
GROUP BY User_ref_id ");