我有

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

这给我返回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` REGEXP 'women.+jacket'
    AND `p`.`status` = 1
ORDER BY p.price_sale ASC

这应该会像以前一样返回相同的结果。但只返回17个结果。我做错什么了?
----------
经过多次搜索,这里是正确的REGEXP
AND p.`keyword` REGEXP '^(.+jacket.+women.+)|^(.+women.+jacket.+).*$'

最佳答案

您的regex和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"字符串中的任意位置和"jacket"字符串中的任意位置
与。
"woman"在开头,然后任何字符在结尾
编辑:
但我该怎么做呢?“女人”在任何地方都穿线,“夹克”在任何地方
在字符串中---使用正则表达式?
一种方法是使用:
SELECT *
FROM tab
WHERE keyword REGEXP '.*women.*'
  AND keyword REGEXP '.*jacket.*';

我确信这种情况存在一种正则表达式。

10-07 15:19