Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择而不分组_Mysql_Sql_Database - Fatal编程技术网

Mysql 选择而不分组

Mysql 选择而不分组,mysql,sql,database,Mysql,Sql,Database,如何在不分组的情况下获得结果 我的桌子 id user_id amount currency_id type status 5 2 2.00 1 0 0 6 3 3.00 1 0 0 4 1 1.00 1 0 0 7 4 4.00 1 0 0 8 5 3.00 1 1 0 我做以下选择 SELECT id, user_id, amount, currency_id, SUM(

如何在不分组的情况下获得结果

我的桌子

id  user_id amount  currency_id type    status
5   2   2.00    1   0   0
6   3   3.00    1   0   0
4   1   1.00    1   0   0
7   4   4.00    1   0   0
8   5   3.00    1   1   0
我做以下选择

SELECT id, user_id, amount, currency_id, SUM( amount ) 
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
如何获得此结果:

id  user_id amount  currency_id SUM( amount )
5   2   2.00    1   0   6.00
6   3   3.00    1   0   6.00
4   1   1.00    1   0   6.00
给你:

SELECT id, user_id, amount, currency_id, t2.total
FROM market, (
    SELECT SUM(amount) AS total
    FROM market
    WHERE amount <=3
    AND type = 0
    AND status = 0
) AS t2
WHERE amount <=3
AND type = 0
AND status = 0
你可以加入

SELECT  id, 
        user_id, 
        amount, 
        currency_id, 
        a.totalAmount
FROM    market
        CROSS JOIN
        (
            SELECT SUM(amount) totalAmount
            FROM    market
            WHERE   amount <=3
                    AND type = 0 
                    AND status = 0
        ) a
WHERE   amount <=3
        AND type = 0 
        AND status = 0
或者使用内联子查询

SELECT  id, 
        user_id, 
        amount, 
        currency_id, 
        (
            SELECT SUM(amount) totalAmount
            FROM    market
            WHERE   amount <=3
                    AND type = 0 
                    AND status = 0
        ) totalAmount
FROM    market
WHERE   amount <=3
        AND type = 0 
        AND status = 0

如果您的目的是返回满足此条件的单个记录并对其进行汇总,而实际上不需要将汇总值作为每行的字段,也不知道为什么要这样做,那么我建议您通过以下方式查看组。。。使用“上卷”修改器。它的工作原理如下:

SELECT id, user_id, SUM(amount) AS `amounts`, currency_id
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
GROUP BY id WITH ROLLUP
id    user_id amounts  currency_id
5     2       2.00    1
6     3       3.00    1
4     1       1.00    1
NULL  3       6.00    1
注意,最后一条记录提供了SUM函数的汇总。还请注意,汇总行中的user_id和currency_id的值是不确定的,因为它们不是GROUP BY或聚合的一部分。因此,它们没有任何意义。

如果您不想分组,请不要使用SUM。您正在尝试获取查询中所有记录的总和吗?我想我不知道每行6.00美元是从哪里来的。
id    user_id amounts  currency_id
5     2       2.00    1
6     3       3.00    1
4     1       1.00    1
NULL  3       6.00    1