【问题标题】:SQL Query similar to Grep functionality类似于 Grep 功能的 SQL 查询
【发布时间】:2015-05-01 02:22:17
【问题描述】:

我有以下 bash 命令:

grep -P 'The Beatles' | wc -l

我需要创建一个模仿相同功能的 SQL 查询。表架构如下:

track_id    
sales_date  
sales_count
title
song_id 
release 
artist_id   
artist_mbid 
artist_name 
duration    
artist_familiarity  
artist_hotttnesss
year

样本数据:

TRZZZZZ12903D05E3A,2014-06-19,79,Infra Stellar,SOZPUEF12AF72A9F2A,Archives Vol. 2,ARBG8621187FB54842,4279aba0-1bde-40a9-8fb2-c63d165dc554,Delerium,495.22893,0.69652442519,0.498471038842,2001

我已经编写了以下代码,但它无法进行区分大小写的匹配。

SELECT Count(track_id) FROM songs WHERE track_id LIKE '%The Beatles%' OR title LIKE '%The Beatles%' OR song_id LIKE '%The Beatles%' OR release LIKE '%The Beatles%' OR artist_id LIKE '%The Beatles%' OR artist_mbid LIKE '%The Beatles%' OR artist_name LIKE '%The Beatles%' OR duration LIKE '%The Beatles%' OR artist_familiarity LIKE '%The Beatles%' OR artist_hotttnesss LIKE '%The Beatles%' OR year LIKE '%The Beatles%'

我尝试使用 COLLATE Latin1_General_CS_ASCOLLATE Latin1_General_BIN 但它给了我以下错误:

ERROR 1273 (HY000) at line 1: Unknown collation: 'Latin1_General_BIN'

无论如何我可以进行区分大小写的匹配吗? 谢谢!

【问题讨论】:

标签: sql bash csv awk grep


【解决方案1】:

在 MySQL 中进行区分大小写比较的一种简单方法是使用binary

SELECT Count(track_id)
FROM songs
WHERE track_id LIKE BINARY '%The Beatles%' OR
      title LIKE BINARY '%The Beatles%' OR
      song_id LIKE BINARY '%The Beatles%' OR
      release LIKE BINARY '%The Beatles%' OR
      artist_id LIKE BINARY '%The Beatles%' OR
      artist_mbid LIKE BINARY '%The Beatles%' OR
      artist_name LIKE BINARY '%The Beatles%' OR
      duration LIKE BINARY '%The Beatles%' OR
      artist_familiarity LIKE BINARY '%The Beatles%' OR 
      artist_hotttnesss LIKE BINARY  '%The Beatles%' OR
      year LIKE BINARY '%The Beatles%';

顺便说一句,这类问题只是说“使用全文索引,使用全文索引”。你可以阅读here

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    相关资源
    最近更新 更多