Mysql REGEXP 2个单词 - 与AND LIKE相同

时间:2015-12-15 08:05:54

标签: mysql sql sql-like regular-language

我有

0 * * * * sync && sysctl -w vm.drop_caches=3

这会让我 158结果

我也有

SELECT p.*, gc.*, g.*, m.*, b.*
FROM (products AS p)
LEFT JOIN merchants AS m
    ON m.merchantId = p.merchantId
LEFT JOIN brands AS b
    ON b.brand = p.brand
LEFT JOIN groups_content gc
    ON p.brand = gc.brandsSelect
LEFT JOIN groups g
    ON g.group_id = gc.group_id
WHERE `p`.`percentOff` > gc.percent_off
    AND `g`.`group_active` = 1
    AND `p`.`price_sale` BETWEEN gc.price_min
        AND gc.price_max
    AND `gc`.`group_content_id` = '180'
    AND `p`.`keyword` LIKE '%women%' AND `p`.`keyword` LIKE '%jacket%'
    AND `p`.`status` = 1
ORDER BY p.price_sale ASC 

这应该像以前一样给我相同的结果。但返回我只是 17结果。我做错了什么?

----------

经过多次搜索,这是正确的REGEXP

SELECT p.*, gc.*, g.*, m.*, b.*
FROM (products AS p)
LEFT JOIN merchants AS m
    ON m.merchantId = p.merchantId
LEFT JOIN brands AS b
    ON b.brand = p.brand
LEFT JOIN groups_content gc
    ON p.brand = gc.brandsSelect
LEFT JOIN groups g
    ON g.group_id = gc.group_id
WHERE `p`.`percentOff` > gc.percent_off
    AND `g`.`group_active` = 1
    AND `p`.`price_sale` BETWEEN gc.price_min
        AND gc.price_max
    AND `gc`.`group_content_id` = '180'
    AND `p`.`keyword` REGEXP 'women.+jacket'
    AND `p`.`status` = 1
ORDER BY p.price_sale ASC

1 个答案:

答案 0 :(得分:1)

你的正则表达式不是等同的陈述:

CREATE TABLE tab(keyword VARCHAR(100));

INSERT INTO tab(keyword)
VALUES ('jacket for women');

SELECT *
FROM tab p
WHERE  `p`.`keyword` LIKE '%women%' AND `p`.`keyword` LIKE '%jacket%';
-- jacket for women

SELECT *
FROM tab p
WHERE p.`keyword` REGEXP 'women.+jacket';
-- (empty)

SqlFiddleDemo

"woman"位于字符串中的任意位置,"jacket"位于字符串

中的任何位置

VS

开头是

"woman",然后是"jacket"最后的任何字符

修改

  

但我如何得到 - "女人"在任何地方的字符串和"夹克"随地   in string ---使用正则表达式?

一种方法是使用:

SELECT *
FROM tab
WHERE keyword REGEXP '.*women.*'
  AND keyword REGEXP '.*jacket.*';

我确信这个案例有一个正则表达式。