限定您的所有列名稱。目前尚不清楚正確的名稱是什麼,但這些都需要表的別名:
關於MySQL
SELECT SC.name as sc, B.name as brand,
COUNT(T.id) as tc,
-------^ Looks like an aggregation query, but there is no `GROUP BY`.
SUM(IF(is_category_present = 1, 1, 0)) as BASE
--------------^
FROM outlet_categories OC INNER JOIN
transactions T
ON OC.outlet_id = T.outlet_id INNER JOIN
outlets O
ON O.id = T.outlet_id INNER JOIN
brands B
ON T.brand_id = B.id INNER JOIN
sale_channels SC
ON SC.id = O.sale_channel_id
WHERE T.month = month;
----------------^
三題:
- 你
IF()
是沒有必要的。
- 我猜
month
是一個變量。如果是這樣,請將其命名爲別的。
- 想必你想要一個
GROUP BY
。
一個更好的版本的查詢:
SELECT SC.name as sc, B.name as brand, COUNT(T.id) as tc,
SUM(??.is_category_present) as BASE
FROM outlet_categories OC INNER JOIN
transactions T
ON OC.outlet_id = T.outlet_id INNER JOIN
outlets O
ON O.id = T.outlet_id INNER JOIN
brands B
ON T.brand_id = B.id INNER JOIN
sale_channels SC
ON SC.id = O.sale_channel_id
WHERE T.month = v_month
GROUP BY SC.name, B.name;
變量month
已更名爲v_month
,所以它是沒有混淆列名。 MySQL會將T.month = month
解釋爲T.month = T.month
- 假設month
僅在T
表中。而且,我猜T.month = T.month
不是你的意圖。
使用別名 – HoneyBadger
需要定義表別名在哪裏cluase WHERE T.month =月 –
可能的重複[Ambiguous column name error](http://stackoverflow.com/questions/318066/ambiguous-column-name -error) – HoneyBadger