SQL - 选择乘法条件

时间:2015-10-19 19:34:07

标签: mysql sql join database-design inner-join

我想使用以下查询选择乘法条件:

SELECT (SELECT count(*)
        FROM users
    )
        as totalusers,
       (SELECT sum(cashedout) 
          FROM users
         ) AS cashedout,
       (SELECT COUNT(*) 
          FROM xeon_users_rented
        ) AS totalbots,
(SELECT sum(value) 
          FROM xeon_stats_clicks
 WHERE typ='3' OR typ='1'
        ) AS totalclicks

上面的查询需要不到一秒钟(准确地说是0.912)才能执行。数以千计的请求会减慢很多事情。

对我来说,合乎逻辑的是这种方法:

SELECT (SELECT count(*), sum(cashedout)
        FROM users
    )
        as totalusers, cashedout,
       (SELECT COUNT(*) 
          FROM xeon_users_rented
        ) AS totalbots,
(SELECT sum(value) 
          FROM xeon_stats_clicks
 WHERE typ='3' OR typ='1'
        ) AS totalclicks

然而,这不起作用,因为我收到以下错误:

 #1241 - Operand should contain 1 column(s)

此外,如何在我的第一个查询中加入另外两个表“xeon_users_rented”和“xeon_stats_clicks”?

2 个答案:

答案 0 :(得分:0)

因为您有多个子查询,所以速度很慢。请尝试使用联接。

此外,您的表格,列列表可以帮助我们更好地为您提供帮助。

答案 1 :(得分:0)

您的第二个查询使用了错误的语法,它应该是

SELECT 
count(*) as totalusers, 
sum(cashedout) cashedout,
(SELECT COUNT(*) FROM xeon_users_rented) AS totalbots,
(SELECT sum(value) FROM xeon_stats_clicks 
 WHERE typ='3' OR typ='1') AS totalclicks
FROM users