1

我有这 2 个查询,我想将它们组合成一个大查询:

SELECT COUNT(*) AS ac, SUM(Views) AS vc FROM tableA

SELECT COUNT(*) AS mc FROM tableB

找不到有关该主题的任何内容,甚至可能吗?

4

3 回答 3

4

您可以使用三个子查询进行操作:

SELECT 
(SELECT COUNT(*) FROM tableA) AS ac,
(SELECT SUM(Views) FROM tableA) AS vc,
(SELECT COUNT(*) FROM tableB) AS mc

这不是很优雅的解决方案,但应该做你需要的。

于 2012-04-06T19:30:34.967 回答
1
SELECT 'tableA' as tablename, COUNT(*) AS count, SUM(Views) AS sum FROM tableA
UNION
SELECT 'tableB' as tablename, COUNT(*) AS count, 0 AS sum FROM tableB
于 2012-04-06T19:30:32.930 回答
0

您可以创建一个返回SELECT COUNT(*) AS mc FROM tableB并在表 A 中使用它的函数

  CREATE FUNCTION Func1() RETURNS INTEGER
  BEGIN
     DECLARE Temp INTEGER;
     SELECT COUNT(*) INTO Temp FROM tableB;
     RETURN IFNULL(Temp, 0);
  END;

  SELECT COUNT(*) AS ac, SUM(Views) AS vc, Func1() as mc FROM tableA
于 2012-04-06T19:32:13.187 回答