比较db值并获取值+ duplicates

时间:2012-03-05 11:26:18

标签: php mysql filter

我正在开发一个用户提交多个日期的项目。然后将日期与当前用户名一起保存在数据库中。 我接下来要做的是获得所有提交日期和提交当前日期的用户的概述。但我的问题是,我只是要显示一个日期,然后是所有选择此唯一日期的用户。像这样:

  • 05.03.2012 = Paul
  • 06.03.2012 = Shane,John
  • 07.03.2012 = John,Irene

而不是像现在这样:

  • 05.03.2012 = Paul
  • 06.03.2012 = Shane
  • 06.03.2012 = John
  • 07.03.2012 = John
  • 07.03.2012 = Irene
你跟我在一起吗?因为那是主要问题。 至于编码部分,我试图这样做:

$table_name = $wpdb->prefix . 'my_db';
$my_date_overview = $wpdb->get_results("
SELECT * FROM $table_name 
WHERE thedate > NOW()
ORDER by thedate ASC");

foreach ( $my_date_overview as $the_dates ) {
echo $the_dates->thedate;
echo ',';
echo $the_dates->username;
}

Db-table值:ID,thedate,username。

我尝试使用“SELECT DISTINCT thedate”,但后来我没有得到用户名。我可以在这里进行这个过滤过程,还是需要创建一个函数?

我试过谷歌但没有任何运气。我没有想法和知识。 谁能帮我吗? 感谢

2 个答案:

答案 0 :(得分:2)

SELECT date, group_concat(username) FROM $table_name 
WHERE thedate > NOW()
GROUP BY thedate
ORDER by thedate ASC"

请注意,默认情况下group_concat的长度是有限的。对于相当数量的名称,yopu必须增加相应变量的值

答案 1 :(得分:-1)

您应该使用

这样的查询
SELECT
GROUP_CONCAT(DISTINCT username
           ORDER BY username DESC SEPARATOR ',') as users,
thedate FROM $table_name 
WHERE thedate > NOW()
GROUP BY thedate
ORDER by thedate ASC