我有這樣結果沒有連接條件
SELECT COUNT(u.user_id) AS count1 FROM users WHERE user_id>1001
UNION
SELECT COUNT(c.hits) AS count2 FROM source c WHERE loginid LIKE 'fb%';
結果查詢
count1
250
56
如何可以把它在一個單行進行這樣
count1 count2
250 56
我有這樣結果沒有連接條件
SELECT COUNT(u.user_id) AS count1 FROM users WHERE user_id>1001
UNION
SELECT COUNT(c.hits) AS count2 FROM source c WHERE loginid LIKE 'fb%';
結果查詢
count1
250
56
如何可以把它在一個單行進行這樣
count1 count2
250 56
select
(SELECT COUNT(u.user_id) AS count1 from users where user_id>1001) as count1,
(select count(c.hits) AS count2 from source c where loginid like 'fb%') as count2
SELECT SUM(count1) AS count1,
SUM(count2) AS count2
FROM(
SELECT COUNT(u.user_id) AS count1, 0 AS count2 from users where user_id>1001
UNION ALL
SELECT 0 AS count1, count(c.hits) AS count2 from source c where loginid like 'fb%';
)a;
這不會工作,因爲上面的查詢不會在內部查詢中找到count2列。 Dumitrescu Bogdan給出了正確的答案 – syncdm2012
它將作爲'UNION'需要具有相同別名的相同數量的列。你爲什麼不嘗試呢?請參閱http://dev.mysql.com/doc/refman/5.6/en/union.html – Omesh
非常感謝你。 – syncdm2012