【问题标题】:Retrieving values based on condition根据条件检索值
【发布时间】:2014-06-19 14:42:06
【问题描述】:

请看下面的查询。

SELECT `p_id`, COUNT(`p_id`) AS OverlapWords, `UniqueWordCount`, 
(COUNT(`p_id`)/`UniqueWordCount`) AS SimScore FROM `key_uniqueword` WHERE `word` IN 
('stand','on') GROUP BY (`p_id`) LIMIT 500

我需要将SimScore 转换为百分比并获取SimScore 小于90% 的所有记录。我怎样才能在 SQL 中做到这一点?上次我这样做时,它给了我一个类似Invalid use of Group function 的错误

【问题讨论】:

    标签: mysql sql select group-by where-clause


    【解决方案1】:

    你试过了吗?

    SELECT `p_id`, COUNT(`p_id`) AS OverlapWords, `UniqueWordCount`, 
           (COUNT(`p_id`)/`UniqueWordCount`) AS SimScore
    FROM `key_uniqueword`
    WHERE `word` IN ('stand','on')
    GROUP BY (`p_id`)
    HAVING SimScore < 0.9
    LIMIT 500;
    

    聚合函数结果的条件属于having 子句,而不是where 子句。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      相关资源
      最近更新 更多