2016-07-13 39 views
-3

我需要得到正確的查詢語法的幫助,我需要從視圖計數是我自己的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 '的條款'

+0

沒有SQL語法選擇..as ..哪裏 – Jens

+0

揭掉,我知道 - 這就是我想要達到與正確的語法 – Jeff

+0

什麼應該是這背後的邏輯是什麼? – Jens

回答

0

只需向第二個左連接添加一個附加條件:「AND v.user_id!=%s」。您也可避免使用帶有參數的「AND v.user_id!= t.user_id」

SELECT SQL_CALC_FOUND_ROWS t.*, 
      Count(DISTINCT l.lead_id) AS leads, 
      Count(DISTINCT v.user_id) AS views 
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 AND v.user_id != %s 
WHERE  t.user_id = %s AND t.published = %s 
GROUP BY t.trade_id 
ORDER BY timestamp DESC 
LIMIT  $start, $perpage 

的其他方式做到這一點,保留post_view數據,使用IF語句,在行級工作

SELECT SQL_CALC_FOUND_ROWS t.*, 
      Count(DISTINCT l.lead_id) AS leads, 
      Count(DISTINCT IF(v.user_id=%s,NULL,v.user_id)) AS views 
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 
-1

我有在你的SQL查詢語法中改變了,但有一個混淆是,你正在使用t.trade_id組,但不使用集合函數中的任何交易表字段。

SELECT SQL_CALC_FOUND_ROWS t.*, 
      Count(DISTINCT l.lead_id) AS leads, 
      Count(DISTINCT v.user_id) AS views 
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 LIKE '%s' 
AND  t.published LIKE '%s' AND v.user_id NOT LIKE '%s' 
GROUP BY t.trade_id 
ORDER BY timestamp DESC 
LIMIT  $start, $perpage 
+0

這將返回沒有結果 - 我只想改變意見數不包括我的ID – Jeff

0

由於我不知道您的特定表格列以及它們之間的關係,所以這有些困難。它看起來像你正在試圖從兩個相關的表中計算數據。

最簡單的方法是使用派生表(子選擇)。

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 
(SELECT subL.trade_id, Count(DISTINCT subL.lead_id) 
    FROM leads) l 
ON  t.trade_id = l.trade_id 
LEFT JOIN 
(SELECT subV.t Count(DISTINCT subV.user_id) 
    FROM post_view subV 
    WHERE subV.user_id != %s) 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 

我還沒有測試過這個。但是這可能會讓你朝着正確的方向前進。

0

您正在根據lead_id & user_id獲取計數。我認爲最好的是將它們分成兩個單獨的查詢。原因是因爲您要求您的WHERE條件僅適用於'user_id'計數&而不適用於'lead_id'。你可以從上面的查詢中刪除行:

Count(DISTINCT v.user_id) AS views where user_id != %s // This wont work

,並創建一個新的查詢,如:

`SELECT SQL_CALC_FOUND_ROWS t.*, 
Count(DISTINCT v.user_id) AS userviews 
FROM  trading t 
LEFT JOIN post_view v ON t.trade_id = v.trade_id 
WHERE  t.user_id = %s AND v.user_id != %s AND t.published = %s 
GROUP BY t.trade_id 
ORDER BY timestamp DESC 
LIMIT  $start, $perpage` 

也請注意,您應不使用MySQL的關鍵字,如views。因此我已將其更改爲userviews