Mysql计数与where子句 - 正确的语法

时间:2016-07-13 12:23:40

标签: mysql

我需要帮助来获取查询的正确语法,我需要从视图计数中排除ID,这是我自己的id。

类似于下面的内容,但首先看出where 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

我现在收到此错误:'on clause'中的未知列'v.trade_id'

4 个答案:

答案 0 :(得分:0)

这有点困难,因为我不知道您的特定表列以及它们是如何相关的。看起来您正在尝试从两个以某种方式相关的表中计算数据。

最简单的方法是使用派生表(subselect)。

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 

我还没有测试过这个。但这可能会让你朝着正确的方向前进。

答案 1 :(得分:0)

您正在根据lead_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`

另请注意,理想情况下,您不应使用views之类的mysql关键字。因此我将其更改为userviews

答案 2 :(得分: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 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

另一种方法是保留post_view数据,使用在行级工作的IF语句

var keys = ["one","two","three","four"];
for (var i = 0; i < keys.length; i++) {
    for (var j = 0; j < 10; j++) {    // Note: you might want to avoid the magic number here
        circle[keys[i]].items.push(createDiv(i * 10 + j + 1));
    }
}

答案 3 :(得分:-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