我需要得到正確的查詢語法的幫助,我需要從視圖計數是我自己的ID排除IDS。Mysql計數與where子句 - 正確的語法
類似於下面的但obvs第一where子句是錯誤的,只是說明我想實現什麼。
SELECT SQL_CALC_FOUND_ROWS t.*,
Count(DISTINCT l.lead_id) AS leads,
Count(DISTINCT v.user_id) AS views where user_id != %s // This wont work
FROM trading t
LEFT JOIN leads l ON t.trade_id = l.trade_id
LEFT JOIN post_view v ON t.trade_id = v.trade_id
WHERE t.user_id = %s AND t.published = %s
GROUP BY t.trade_id
ORDER BY timestamp DESC
LIMIT $start, $perpage
更新:
使用子查詢
SELECT SQL_CALC_FOUND_ROWS t.*,
Count(DISTINCT l.lead_id) AS leads
FROM trading t
LEFT JOIN leads l
ON t.trade_id = l.trade_id
LEFT JOIN
(SELECT Count(DISTINCT user_id) AS views
FROM post_view WHERE user_id != %s) as v
ON t.trade_id = v.trade_id
WHERE t.user_id = %s
AND t.published = %s
GROUP BY t.trade_id
ORDER BY timestamp DESC
LIMIT $start, $perpage
我現在收到此錯誤的嘗試:未知列在「v.trade_id '的條款'
沒有SQL語法選擇..as ..哪裏 – Jens
揭掉,我知道 - 這就是我想要達到與正確的語法 – Jeff
什麼應該是這背後的邏輯是什麼? – Jens