简体   繁体   English

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

[英]Mysql REGEXP 2 words — Same as AND LIKE

I have 我有

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 

this return me 158 results 这返回我158结果

Also I have 我也有

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

This should return me same result like previous. 这应该给我像以前一样的结果。 But returns me just 17 results . 但是只返回17个结果给我。 What I am doing wrong? 我做错了什么?

---------- ----------

AFTER MANY SEARCHES here it is the right REGEXP 经过多次搜索后,这里才是正确的REGEXP

AND p.`keyword` REGEXP '^(.+jacket.+women.+)|^(.+women.+jacket.+).*$'

Your regex and like are not equivalent statements: 您的正则表达式和like不是等价的陈述:

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" anywhere in string and "jacket" anywhere in string 字符串中任何位置的"woman"和字符串中任何位置的"jacket"

vs.

"woman" at the beginning, then any characters then "jacket" at the end "woman" ,然后是任何字符,然后是"jacket"

EDIT: 编辑:

But How do I get -- "woman" anywhere in string and "jacket" anywhere in string --- using regular expression? 但是,如何使用正则表达式-在字符串中的任何位置“女人”和在字符串中的任何位置“外套”-使用正则表达式?

One way is to use: 一种方法是使用:

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

I am sure there exists one regex for this case. 我敢肯定这种情况下存在一个正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM