在mysql中组合两个表以获取最近的活动功能

时间:2012-12-26 05:29:34

标签: php mysql

在我的页面中,我有一个名为Recent activity的选项卡,其中我必须显示两种不同类型的操作

  • 最近添加的选择
  • 最近投票的选择

je_addchoice的表格结构

je_addpoll table

poll_id | user_id | poll_name | category_id | start_date | end_date

  1     |   20    |Naturalflrs|    18       | 2012-12-03 | 2095-12-25

je_addchoice表

choice_id | poll_id | choice_creator_id | choice_name | choice_image | description | ctime

     1    |    1    |        20         |  Greenish   | forest.jpg   | forest      |135453

je_user_vote table

vote_id | user_id | poll_id | choice_id | datetime_voted | user_type

  12    |  31     |    1    |   1       |12-12-2606:23:17|  normal

我已经显示结果页面,如下所示

The recently added choices are displayed

使用查询

显示上述结果
$result=mysql_query("SELECT * FROM je_addchoice, je_addpoll where je_addpoll.start_date <= '$check_date' AND je_addpoll.end_date >='$check_date'  AND je_addpoll.poll_id=je_addchoice.poll_id order by je_addchoice.choicecreationtime desc");

两个表

1)je_addpoll(民意调查主表)

2)je_addchoice(为民意调查添加选项)

但我想在这里做的是如果任何用户投票支持民意调查,它将存储到

je_user_vote表如上图所示。

我想在同一个标​​签中显示最近投票的选项

1 个答案:

答案 0 :(得分:1)

试试这个

desc

之前datetime_voted使用je_addchoice.choicecreationtime desc订单 像这样。

$result=mysql_query("SELECT * FROM (SELECT P.poll_id,P.user_id,P.poll_name,P.category_id,P.start_date,P.end_date,C.choice_id,C.choice_creator_id,C.choice_name,C.choice_image,C.description,C.ctime FROM je_addchoice C, je_addpoll P where P.start_date <= '$check_date' AND P.end_date >='$check_date'  AND P.poll_id=C.poll_id order by C.choicecreationtime desc) N,je_user_vote U WHERE U.poll_id=N.poll_id order by U.datetime_voted desc,N.choicecreationtime desc");

OR

$result=mysql_query("SELECT N.poll_id,N.user_id,N.poll_name,N.category_id,N.start_date,N.end_date,N.choice_id,N.choice_creator_id,N.choice_name,N.choice_image,N.description,N.ctime FROM (SELECT P.poll_id,P.user_id,P.poll_name,P.category_id,P.start_date,P.end_date,C.choice_id,C.choice_creator_id,C.choice_name,C.choice_image,C.description,C.ctime FROM je_addchoice C, je_addpoll P where P.start_date <= '$check_date' AND P.end_date >='$check_date'  AND P.poll_id=C.poll_id order by C.choicecreationtime desc) N,je_user_vote U WHERE U.poll_id=N.poll_id order by U.datetime_voted desc,N.choicecreationtime desc");