dongzanxun2790 2011-09-14 01:32
浏览 52
已采纳

PHP / MySQL检索每个用户的最后一个操作以制作列表

i m developing a dynamic website but i have a problem with a query. I m trying to build a map dynamically by retrieving date from the database. I enclose the db structure:

countries (code, name)

data (user_id, dateaction, container)

users (user_id, country_code)

I want to display a list of the AVG container for each country, but the average value should be calculated from last action of each user and the data should be submitted the current day. In the case of non-value of "container" for any country the value is 0. I tried many queries but i couldnt achieve the target. I attach some of the queries i tried.

$query = "SELECT AVG(data.container), data.user_id, users.country_code ".
"FROM data, users ".
"WHERE data.user_id = users.user_id AND dateaction >= '".date('Y-m-d').' 00:00:00'."' AND date < '".date('Y-m-d').' 23:59:59'."'".
"GROUP BY users.country_code ";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    $cid=$row['country_code'];
    $queryy=mysql_query("select * FROM Countries WHERE country_code='$cid'") or die("Error Occured,plz try again1");
    $rowy = mysql_fetch_array( $queryy );
    $cname=$rowy['country_name'];
    echo '<area title="'.strtoupper($cname).'" mc_name="'.strtoupper($cid).'"></area>'; }

Please may you help me to solve this logical problem?

  • 写回答

1条回答 默认 最新

  • duan1930 2011-09-14 01:43
    关注
    $query = "SELECT a.user_id, ifnull(a.country_code, 0) as country_code, avg(a.container) ".
    "FROM data a INNER JOIN ".
    "(SELECT data.user_id, max(dateaction) dateaction ".
    "FROM data".
    "WHERE dateaction >= '".date('Y-m-d').' 00:00:00'."' AND date < '".date('Y-m-d').' 23:59:59'."'".
    "GROUP BY data.user_id) b ".
    "ON a.user_id = b.user_id and a.date_action = b.date_action ".
    "GROUP BY a.user_id, ifnull(a.country_code, 0) "
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口