gpt4 book ai didi

Mysql REGEXP 2 words -- 与 AND LIKE 相同

转载 作者:行者123 更新时间:2023-11-28 23:41:22 26 4
gpt4 key购买 nike

我有

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 个结果。我做错了什么?

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

经过多次搜索,这是正确的正则表达式

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

最佳答案

你的正则表达式等不是等价的语句:

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" 开头,然后是任何字符,最后是 "jacket"

编辑:

But How do I get -- "woman" anywhere in string and "jacket" anywhere in string --- using regular expression?

一种方法是使用:

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

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

关于Mysql REGEXP 2 words -- 与 AND LIKE 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com