如何将两个查询与不同列合并为一个?

时间:2016-03-24 21:08:27

标签: mysql

如何将这两个查询合并到一个表中。它们是两个不同的结果,所以我需要它们是两个不同的列。我已经尝试过UNION,但是将它们放在一列中。

SELECT COUNT(*) AS Number_Customers FROM customer
Where cus_balance > 0;
Select SUM(cus_balance) AS Total_Balances
FROM customer;

1 个答案:

答案 0 :(得分:0)

将它们重写为子查询,并使用DUAL作为虚拟表。

SELECT
   (SELECT COUNT(*) FROM customer WHERE cus_balance > 0) AS Number_Customers,
   (SELECT SUM(cus_balance) FROM customer) AS Total_Balances
FROM DUAL;

如果WHERE条款可能相同,那么你可以这样写:

SELECT COUNT(*) AS Number_Customers,
       SUM(cus_balance) AS Total_Balances
FROM Cusomers
WHERE cus_balance > 0;