【问题标题】:count values where column doesn't contain underscore计算列不包含下划线的值
【发布时间】:2014-01-24 04:30:51
【问题描述】:

我有以下查询,我想在我的计数中修改它,而不是movies,它是不带下划线的值。对于第二个计数,我想要包含下划线的值。

这是原始查询:

select document
, (count(distinct (case when table_name = 'movies' then keyword else null end)) * 100)
    + (count(distinct (case when table_name = 'movie_summaries' then keyword else null end)) * 50)
    as weight
from indexes where keyword in('the', 'hobbit')
group by document
order by weight desc limit 20

前3位权重如下:

  • 300
  • 250
  • 200

我修改了它,所以它看起来像这样,但它似乎不起作用:

select document
, (count(distinct (case when table_name not like '%_%' then keyword else null end)) * 100)
    + (count(distinct (case when table_name like '%_%' then keyword else null end)) * 50)
    as weight
from indexes where keyword in('the', 'hobbit')
group by document
order by weight desc limit 20

现在排名前 3 的权重是:

  • 100
  • 100
  • 100

是什么导致了这种变化?

CREATE TABLE `indexes` (
    `keyword` CHAR(15) NOT NULL,
    `document` INT(10) NOT NULL,
    `position` INT(11) NOT NULL,
    `table_name` CHAR(15) NOT NULL,
    PRIMARY KEY (`keyword`, `document`, `position`, `table_name`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;

【问题讨论】:

    标签: mysql sql escaping sql-like


    【解决方案1】:

    核心问题是下划线是sql之类的小丑字符。你应该像 \_ 一样逃避它。

    更多详情:Why does "_" (underscore) match "-" (hyphen)?

    【讨论】:

    • 啊,解决了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多