mysql - 在执行 case 语句之前对 sql 查询进行排序

标签 mysql

我正在尝试计算我需要创建的图表的余额,但是我遇到了一个问题,即数据未在日期后排序,如果我按日期排序,它将在我计算完平衡,因此平衡仍然是错误的。在该日期之前订购查询后,如何计算余额?

http://sqlfiddle.com/#!2/374b9/6

测试数据:

CREATE TABLE betting 
    (
     id int auto_increment primary key,
     date DATETIME, 
     odds varchar(20),
     status varchar(30)
    );

INSERT INTO betting
(id, date, odds, status)
VALUES
('1', '2015-02-08 20:27:44', '1.70', 'wrong'),
('2', '2015-02-08 13:22:17', '3.05', 'correct'),
('3', '2015-02-09 16:40:45', '2.20', 'correct'),
('4', '2015-02-10 13:58:24', '1.33', 'correct'),
('5', '2015-02-12 11:37:51', '1.35', 'correct'),
('6', '2015-02-12 10:24:13', '1.38', 'correct');


SET @balance = 0;
SELECT date, odds, status, CASE WHEN status =  'wrong'
            THEN @balance := @balance -100
            ELSE @balance := @balance + ( odds * 100 - 100 ) 
            END as balance
from betting

期望的平衡

205
105
225
258
293
331

我创建了一个 SQLFiddle 来简化它。


额外信息

由于存在一些混淆,我决定添加真正的查询,由于它是 wordpress,所以会稍微长一些。

    SET @balance = 0;
     SELECT odds.meta_value AS odds, stat.meta_value AS stats, posts.post_date, 
        CASE WHEN stat.meta_value =  'wrong'
        THEN @balance := @balance -100
        ELSE @balance := @balance + ( odds.meta_value * 100 - 100 ) 
        END as balance
                FROM wp_t3a673_posts posts
                LEFT JOIN wp_t3a673_postmeta stat ON posts.ID = stat.post_id
                AND stat.meta_key =  'status'
                LEFT JOIN wp_t3a673_postmeta odds ON posts.ID = odds.post_id
                AND odds.meta_key =  'odds'
                LEFT JOIN wp_t3a673_term_relationships tr ON posts.ID = tr.object_id
                LEFT JOIN wp_t3a673_term_taxonomy t ON tr.term_taxonomy_id = t.term_taxonomy_id
                WHERE (
                stat.meta_value =  'correct'
                OR stat.meta_value =  'wrong'
                )
                AND posts.post_status =  'publish'
                AND t.taxonomy =  'category'
                AND (
                t.term_id =4
                OR t.term_id =5
                OR t.term_id =6
                )
        ORDER BY posts.post_date

结果如下。在这里你可以清楚地看到第一个余额不正确:它应该是 -100,但返回 105,这是因为它在计算余额后排序:

enter image description here

如果我只是 -100 无论是错误还是正确你甚至可以清楚地看到问题

enter image description here

最佳答案

问题可能是,由于连接,变量在较早阶段被评估。

尝试嵌入主查询,强制 mysql 最后计算变量:

SET @balance = 0;
 SELECT *, 
    CASE WHEN stats =  'wrong'
    THEN @balance := @balance -100
    ELSE @balance := @balance + ( odds * 100 - 100 ) 
    END as balance
    FROM (SELECT odds.meta_value AS odds, stat.meta_value AS stats, posts.post_date
            FROM wp_t3a673_posts posts
            LEFT JOIN wp_t3a673_postmeta stat ON posts.ID = stat.post_id
            AND stat.meta_key =  'status'
            LEFT JOIN wp_t3a673_postmeta odds ON posts.ID = odds.post_id
            AND odds.meta_key =  'odds'
            LEFT JOIN wp_t3a673_term_relationships tr ON posts.ID = tr.object_id
            LEFT JOIN wp_t3a673_term_taxonomy t ON tr.term_taxonomy_id = t.term_taxonomy_id
            WHERE (
            stat.meta_value =  'correct'
            OR stat.meta_value =  'wrong'
            )
            AND posts.post_status =  'publish'
            AND t.taxonomy =  'category'
            AND (
            t.term_id =4
            OR t.term_id =5
            OR t.term_id =6
            )
    ORDER BY posts.post_date
    ) tt

关于mysql - 在执行 case 语句之前对 sql 查询进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28558516/

相关文章:

mysql - LINUX MINT mysql

php - 无法使用 PHP 从 MySQL 获取数据

mysql - 基于列值的条件 JOIN

c# - PHP 中的 SQL 更新

javascript - 为什么我在 Node 中使用 mysql 的 connection.query 不起作用?

php - 我无法将数据添加到我的 mysql 数据库中的表中

mysql - NOT NULL 单元格的 SQL 语法

php - Laravel 5.1 ACL 使用 spatie/laravel-permission

javascript - 从node.js中的其他文件调用函数和变量

mysql - nodejs-mysql中多个占位符的一个值