仅运行包含子查询的查询

时间:2014-05-29 13:23:07

标签: sql

我有一个只包含子查询的SQL查询,但我无法运行它我得到一个SQL错误,运行只包含子查询的查询的正确语法是什么??

查询:

SELECT COUNT(id) AS 'totalNumOfCustomers'
FROM customers 
WHERE customers.isDemo =0 
AND customers.isLead=0 
AND regTime < '2012-01-01'

SELECT COUNT(id) AS 'totalNumOfCustomersPerMonth' 
FROM customers 
WHERE customers.isDemo =0 
AND customers.isLead=0 
AND regTime >='2012-01-01' 
AND regTime < '2012-02-01'

SELECT COUNT(positions.id) AS 'totalPositions',
ROUND(SUM(amount),2) - ROUND(SUM(payout),2) AS 'grossIncome'  
FROM positions  
LEFT JOIN customers ON positions.customerId = customers.id 
WHERE date >= '2012-01-01' 
AND date < '2012-02-01' 
AND customers.isDemo=0 
AND customers.isLead=0 
AND status != 'canceled'

我在WorkBench中运行它,错误信息是:

Error Code: 1064. You Have an error in your SQL syntax

4 个答案:

答案 0 :(得分:1)

我认为你得到错误,因为每个查询的结尾都没有;蚂蚁。没有错误信息,所以只是猜测

答案 1 :(得分:1)

这个怎么样

select 

(SELECT COUNT(id)
FROM customers 
WHERE customers.isDemo =0 
AND customers.isLead=0 
AND regTime < '2012-01-01') as totalNumOfCustomers,

(SELECT COUNT(id) 
FROM customers 
WHERE customers.isDemo =0 
AND customers.isLead=0 
AND regTime >='2012-01-01' 
AND regTime < '2012-02-01') as totalNumOfCustomersPerMonth,

(SELECT COUNT(positions.id)
FROM positions  
LEFT JOIN customers ON positions.customerId = customers.id 
WHERE date >= '2012-01-01' 
AND date < '2012-02-01' 
AND customers.isDemo=0 
AND customers.isLead=0 
AND status != 'canceled') as totalPositions,

(SELECT ROUND(SUM(amount),2) - ROUND(SUM(payout),2)
FROM positions  
LEFT JOIN customers ON positions.customerId = customers.id 
WHERE date >= '2012-01-01' 
AND date < '2012-02-01' 
AND customers.isDemo=0 
AND customers.isLead=0 
AND status != 'canceled') as grossIncome;

答案 2 :(得分:0)

您可以通过将这些查询放入from子句并使用CROSS JOIN组合来运行这些查询:

SELECT *
FROM (SELECT COUNT(id) totalNumOfCustomers
        FROM customers 
        WHERE customers.isDemo =0 
        AND customers.isLead=0 
        AND regTime < '2012-01-01'
       ) t1 CROSS JOIN
       (SELECT COUNT(id) as totalNumOfCustomersPerMonth
        FROM customers 
        WHERE customers.isDemo =0 
        AND customers.isLead=0 
        AND regTime >='2012-01-01' 
        AND regTime < '2012-02-01'
       ) t2 CROSS JOIN
       (SELECT COUNT(positions.id) AS totalPositions,
               ROUND(SUM(amount),2) - ROUND(SUM(payout),2) AS grossIncome
        FROM positions LEFT JOIN
             customers
             ON positions.customerId = customers.id 
        WHERE date >= '2012-01-01' 
        AND date < '2012-02-01' 
        AND customers.isDemo=0 
        AND customers.isLead=0 
        AND status <> 'canceled'
      ) t3;

注意:不要对列和表别名使用单引号。这将导致混乱。单引号只能用于字符串和日期常量。

答案 3 :(得分:0)

您可以轻松地将两个第一个查询表达为一个查询:

SELECT COUNT(case when regTime < '2012-01-01' end ) AS totalNumOfCustomers
     , COUNT(case when >='2012-01-01' AND regTime < '2012-02-01' end ) AS  totalNumOfCustomersPerMonth
FROM customers 
WHERE customers.isDemo =0 
  AND customers.isLead=0 ;

正如其他人所建议的那样,您可以使用交叉联接来使第三个查询发挥作用