如果在SQL SELECT上重复,则仅返回1项,并使用PHP语句进行检查

时间:2013-10-09 17:40:54

标签: php mysql sql select

我是新来的,但让我试着解释一下这个问题:

我有一个现金返还系统,并且有优惠券,因此,当我打电话显示所有活跃的优惠券时,它会让零售商通过他们的ID来回报:

$from = ($page-1)*$results_per_page;
$where = " (start_date<=NOW() AND (end_date='0000-00-00 00:00:00' OR end_date > NOW())) AND status='active'";

$query = "SELECT c.*,cashback,texto_livre, DATE_FORMAT(c.end_date, '%d %b %Y') AS coupon_end_date, UNIX_TIMESTAMP(c.end_date) - UNIX_TIMESTAMP() AS time_left, r.image, r.title FROM cashbackengine_coupons c LEFT JOIN cashbackengine_retailers r ON c.retailer_id=r.retailer_id WHERE (c.start_date<=NOW() AND (c.end_date='0000-00-00 00:00:00' OR c.end_date > NOW())) AND c.status='active' AND r.status='active' ORDER BY $rrorder $rorder LIMIT $from, $results_per_page";
$total_result = smart_mysql_query("SELECT * FROM cashbackengine_coupons WHERE $where ORDER BY title ASC");
$total = mysql_num_rows($total_result);

$result = smart_mysql_query($query);
$total_on_page = mysql_num_rows($result); 

问题在于其返回的双重零售商因为同一零售商有多张优惠券。我需要一种方法来检查,如果零售商ID相同,只显示1而不是全部。

希望我很清楚,如果我需要更好地解释,请告诉我,谢谢你们!

更新(GROUP BY)

$from = ($page-1)*$results_per_page;

$where = " (start_date<=NOW() AND (end_date='0000-00-00 00:00:00' OR end_date > NOW())) AND status='active'";

$query = "SELECT c.*,cashback,texto_livre, DATE_FORMAT(c.end_date, '%d %b %Y') AS coupon_end_date, UNIX_TIMESTAMP(c.end_date) - UNIX_TIMESTAMP() AS time_left, r.image, r.title FROM cashbackengine_coupons c LEFT JOIN cashbackengine_retailers r ON c.retailer_id=r.retailer_id WHERE (c.start_date<=NOW() AND (c.end_date='0000-00-00 00:00:00' OR c.end_date > NOW())) AND c.status='active' AND r.status='active' ORDER BY $rrorder $rorder LIMIT $from, $results_per_page GROUP BY retailer_id";
$total_result = smart_mysql_query("SELECT * FROM cashbackengine_coupons WHERE $where ORDER BY title ASC");
$total = mysql_num_rows($total_result);

$result = smart_mysql_query($query);
$total_on_page = mysql_num_rows($result);

0 个答案:

没有答案