SQL修改日期的行总和

时间:2014-01-23 19:05:50

标签: php mysql sql date

我有一个拍卖网站,我正在尝试创建一个简单的“当前网站上的总出价”PHP页面。

在我的SQL数据库中,我有:

+-----------+-------------+------------+---------------------+
| AuctionID | InitialBid  | BidPrice   | Modified            |
+-----------+-------------+------------+---------------------+
| 190       | 10.00       | 10.00      | 2014-01-23 16:33:19 |
+-----------+-------------+------------+---------------------+
| 190       | 15.00       | 15.00      | 2014-01-23 16:33:35 |
+-----------+-------------+------------+---------------------+
| 191       | 10.00       | 10.00      | 2014-01-23 17:51:55 |
+-----------+-------------+------------+---------------------+
| 191       | 15.00       | 15.00      | 2014-01-23 17:52:17 |
+-----------+-------------+------------+---------------------+

我只想要具有最新修改日期的最高出价的总和。总数应该加起来为30。

我尝试了这个,但它总共给了我50个,只是加了BidPrice的所有内容。我不知道如何让它选择最新的修改价格给我正确的总数30:

$sql = "SELECT SUM(bid_price) FROM sto8v_bid_log"; 
$sql_result = mysql_query($sql); 
$row = mysql_fetch_array($sql_result); 
echo "Total Bids Currently $ " . $row[0];

这是我从用户Tin Tran尝试的新PHP代码;它适用于SQL Fiddle,但不适用于我的网站。

<?php

$link = mysql_connect("localhost", "login", "pass");
mysql_select_db("database", $link);

SELECT SUM(bid_price) AS Total
FROM sto8v_bid_log T1
INNER JOIN
(SELECT auction_id,MAX(modified) AS maxmodified
FROM sto8v_bid_log
GROUP BY auction_id)T2
ON T1.auction_id = T2.auction_id
AND T1.modified = T2.maxmodified

?>

我收到此错误:

Parse error: syntax error, unexpected T_STRING in /home3/account/public_html/grandtotal.php on line 6

这是我的db表,其中包含正确的列名:

+------------+--------+-------------+-----------+---------------------+
| auction_id | userid | initial_bid | bid_price | modified            |
+------------+--------+-------------+-----------+---------------------+
| 190        | 770    | 10.00       | 10.00     | 2014-01-23 16:33:19 |
+------------+--------+-------------+-----------+---------------------+
| 190        | 771    | 15.00       | 15.00     | 2014-01-23 16:33:35 |
+------------+--------+-------------+-----------+---------------------+
| 191        | 753    | 10.00       | 10.00     | 2014-01-23 17:51:55 |
+------------+--------+-------------+-----------+---------------------+
| 191        | 771    | 15.00       | 15.00     | 2014-01-23 17:52:17 |
+------------+--------+-------------+-----------+---------------------+

2 个答案:

答案 0 :(得分:0)

尝试此操作以获取每个竞价ID的最高价格,按每次竞价分组并进行修改。

select AuctionID, max(BidPrice), Modified FROM sto8v_bid_log GROUP BY 1, 2

由于您要求明确报告:foreach拍卖,提供或支付的最高价格以及提供/支付的日期时间,因此不可能需要30的sumarized值。

答案 1 :(得分:0)

SELECT SUM(Bid_Price) as Total
FROM sto8v_bid_log T1
INNER JOIN
   (SELECT AuctionId,MAX(Modified) as MaxModified
    FROM sto8v_bid_log
    GROUP BY AuctionId)T2 
ON T1.AuctionId = T2.AuctionId
AND T1.Modified = T2.MaxModified

您必须为每个AuctionId获取MAX(已修改),然后将JOIN返回到您的表中以获取Bid_Price,然后使用SUM()它。

sqlFiddle

下面的php代码

<?php
    $link = mysqli_connect("localhost","login","pass","database");
    if (mysqli_connect_errno($link))
    {
        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
    }
    $sql = "SELECT SUM(bid_price) AS total
                FROM sto8v_bid_log T1
                INNER JOIN
                (SELECT auction_id,MAX(modified) AS maxmodified
                FROM sto8v_bid_log
                GROUP BY auction_id)T2
                ON T1.auction_id = T2.auction_id
                AND T1.modified = T2.maxmodified";
    $result = mysqli_query($link,$sql); //this line executes query

    // error checking
    if (!$result){
        printf("Error: %s\n%s\n", mysqli_sqlstate($link),mysqli_error($link));
    }

    // 
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
        echo "Total Bids Currently $ " . $row['total'];
    }
?>