這裏是我的查詢:聯盟選擇列不匹配
SELECT
FROM_UNIXTIME(date_added, '%m-%d-%Y') AS formatted_date,
SUM(tb =1) AS sum_users,
SUM(tb =2) AS sum_links,
SUM(tb =3) AS sum_ads,
SUM(tb =4) AS sum_actions
FROM (
SELECT date_added, 1 AS tb
FROM users_list WHERE 1=1
UNION ALL
SELECT date_added, 2
FROM users_links WHERE 1=1
UNION ALL
SELECT date_served, 3
FROM ads_served WHERE 1=1
UNION ALL
SELECT date_served, 4
FROM actions WHERE 1=1
) AS t
GROUP BY formatted_date
ORDER BY formatted_date DESC
這裏是我的表中的數據:
users_list
id date_added
1 1234567890
2 1334567890
3 1434567890
users_links
id date_added
1 1244567890
2 1354567890
3 1464567890
ads_served
個id date_served revenue
1 1234567891 0.01
2 1334567892 0.02
3 1434567893 0.02
行動
id date_served
1 1234561890
2 1334562890
3 1434563890
我想總結的收入FORMATTED_DATE在ads_served表作爲輸出查詢第6列。我失去了從哪裏開始。如果我將總和(收入)添加到工會選擇我得到一個「列不匹配」錯誤。
doh!應該想到這一點。這工作! – reefine 2011-05-03 00:08:49
「as total」後面應該有一個逗號btw – reefine 2011-05-03 00:09:14
爲了這個例子,我忘了編輯WHERE 1 = 1。在代碼中,應用於每個SELECT語句的過濾器。我使用「1 = 1」來確保下面的過濾器在前面有「和」以便更容易地應用過濾器。 – reefine 2011-05-03 00:10:45