doob0526 2011-02-22 15:09
浏览 45
已采纳

MySQL和Regex:仅匹配整个单词还是跳过URL?

I use the query below for searching,

SELECT
pg_id AS ID, 
pg_url AS URL,
pg_title AS Title,
pg_content_1 AS Content_1,
pg_content_2 AS Content_2,
parent_id AS Parent_id,

EXTRACT(DAY FROM pg_created) AS Date,
EXTRACT(MONTH FROM pg_created) AS Month,
EXTRACT(YEAR FROM pg_created) AS Year

FROM root_pages

WHERE root_pages.pg_cat_id = '2'
AND root_pages.parent_id != root_pages.pg_id
AND root_pages.pg_hide != '1'
AND root_pages.pg_url != 'cms'
AND root_pages.pg_content_1 REGEXP '[[:<:]]".$search."[[:>:]]'
OR root_pages.pg_content_2 REGEXP '[[:<:]]".$search."[[:>:]]'

ORDER BY root_pages.pg_created DESC

I works fine but I don't want it to search the keyword in the URL address such as,

If I search for the keyword of home, the query will search any matches of 'home' in the URLs below and return them as the result too:

http://epp.eurostat.ec.europa.eu/xx/eurostat/home/

http://ec.europa.eu/home-affairs/doc_centre/xx.pdf

How can I fix the query so that it won't match the keyword in an URL or it should match the whole word only?

Thanks.

  • 写回答

2条回答 默认 最新

  • douqin7086 2011-02-23 15:52
    关注

    EDIT:

    Found my own solution and it seems to work fine as it skips searching the text in the column with http://

    SELECT
    pg_id AS ID, 
    pg_url AS URL,
    pg_title AS Title,
    pg_content_1 AS Content_1,
    pg_content_2 AS Content_2,
    parent_id AS Parent_id,
    
    EXTRACT(DAY FROM pg_created) AS Date,
    EXTRACT(MONTH FROM pg_created) AS Month,
    EXTRACT(YEAR FROM pg_created) AS Year
    
    FROM root_pages
    
    WHERE root_pages.pg_cat_id = '2'
    AND root_pages.parent_id != root_pages.pg_id
    AND root_pages.pg_hide != '1'
    AND root_pages.pg_url != 'cms'
    AND root_pages.pg_content_1 LIKE '%".$search."%'
    OR root_pages.pg_content_2 LIKE '%".$search."%'
    
    AND root_pages.pg_content_1 NOT LIKE '%http://%'
    AND root_pages.pg_content_2 NOT LIKE '%http://%'
    
    ORDER BY root_pages.pg_created DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记