我有这 2 个查询,我想将它们组合成一个大查询:
SELECT COUNT(*) AS ac, SUM(Views) AS vc FROM tableA
和
SELECT COUNT(*) AS mc FROM tableB
找不到有关该主题的任何内容,甚至可能吗?
您可以使用三个子查询进行操作:
SELECT
(SELECT COUNT(*) FROM tableA) AS ac,
(SELECT SUM(Views) FROM tableA) AS vc,
(SELECT COUNT(*) FROM tableB) AS mc
这不是很优雅的解决方案,但应该做你需要的。
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
您可以创建一个返回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