如果Id匹配则Php / mysql插入信息Else插入不同的信息

时间:2012-05-05 15:12:23

标签: php mysql

好的,所以这个已被修改,还有3个表板,帐号,日志

感谢Bryan Moyles他给我一个顿悟

这是我的代码

$query=$S->query("
    SELECT
        `TBL`.`LAcc`,
        `TBL`.`AId`,
        `TBL`.`Add`,
        `TBL`.`Lost`,
        `TBL`.`LDate`,
        `TBL`.`FDate`

        FROM (
            SELECT 
                (SELECT SUM(`Add`) FROM `m_rules` GROUP BY `Group`) AS `Add`,
                `S`.`Account` as `LAcc`,
                `P`.`Id` as `AId`,
                SUM(`S`.`Loss`*`S`.`Multiple`) AS `Lost`,
                MAX(`S`.`Timestamp`) AS `LDate`,
                MIN(`S`.`Timestamp`) AS `FDate`
                    FROM `log` AS S,`account` AS P

            GROUP BY `P`.`Id`
        ) as `TBL`
        WHERE `TBL`.`FDate` <= NOW()
        AND `TBL`.`LDate` >= DATE_SUB(NOW(), INTERVAL 1 DAY)

") or die(mysql_error());

    while($Log=$S->fetch($query)) { 

        if($Log['AId']==$Log['LAcc']) {
            $Score=$Log['Add']-$Log['Lost'];
        } else {
            $Score='Bonus';
        }

        /*
            echoing to see results
        */
        echo $Log['AId'].' : '.$Score.'<br />';

        /* 
        This is what the query would look like Thanks to Bryan Moyles,

        $S->query("INSERT INTO `board` (`Account`,`Score`, `Date`)VALUES('".$Log['Id']."', '".$Score."', 'NOW()') ON DUPLICATE KEY UPDATE Score = '".$Score."'");
        */
    }

您可以在此处查看所有结果:按帐户ID分组

Account Id:4, Log Account:5, Add: 97.64236, Lost:141.11371 LastDate:2012-05-04 22:07:46, FirstDate:2012-05-04 22:05:48, Score:Bonus
Account Id:5, Log Account:5, Add: 97.64236, Lost:141.11371 LastDate:2012-05-04 22:07:46, FirstDate:2012-05-04 22:05:48, Score:-43.47135
Account Id:6, Log Account:5, Add: 97.64236, Lost:141.11371 LastDate:2012-05-04 22:07:46, FirstDate:2012-05-04 22:05:48, Score:Bonus
Account Id:7, Log Account:5, Add: 97.64236, Lost:141.11371 LastDate:2012-05-04 22:07:46, FirstDate:2012-05-04 22:05:48, Score:Bonus
Account Id:8, Log Account:5, Add: 97.64236, Lost:141.11371 LastDate:2012-05-04 22:07:46, FirstDate:2012-05-04 22:05:48, Score:Bonus

GROUP BY日志帐户ID

Account Id:4, Log Account:4, Add: 97.64236, Lost:119.67095 LastDate:2012-05-04 22:07:46, FirstDate:2012-05-04 22:06:29, Score:-22.02859
Account Id:4, Log Account:5, Add: 97.64236, Lost:542.30295 LastDate:2012-05-04 22:06:54, FirstDate:2012-05-04 22:05:48, Score:Bonus
Account Id:4, Log Account:6, Add: 97.64236, Lost:43.59465 LastDate:2012-05-04 22:07:06, FirstDate:2012-05-04 22:07:06, Score:Bonus

我发现问题不确定解决方案..

解决!

我替换了

FROM `log` AS S,`account` AS P

使用

FROM `account` AS `P` LEFT JOIN `log` AS `S` ON `P`.`Id`=`S`.`Account`

在原始查询中(在我修改并在此处更改之前)我拥有该帐户并与P一起反向登录。Id = S。{{1}这不起作用,所以我只是交换了日志和帐户,它的工作..不能相信这是问题,但谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我不确定您是如何遇到日志不存在的情况,因为您正在根据查询结果构建数据。但是,您正在插入主键,您可以捕获重复键错误,并相应地运行更新。

查看这篇文章 http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

$S->query("INSERT INTO `board` (`Account`,`Score`, `Date`)VALUES('".$Log['Id']."', '".$Score."', 'NOW()') ON DUPLICATE KEY UPDATE Score = '".$Score."'");