过滤器的MySQL查询

问题描述:

我有这个疑问:过滤器的MySQL查询

select id, store_id, name from ads 
where name regexp '^.*(Massa+.*effect+)|(Glutamina+.*Black+.*Belt+).*$'; 

结果是:

id | store_id | name 
----|----------|----------------- 
94 |  9 | Massa effect 
96 |  9 | Glutamina black belt 
131 |  1 | Glutamina black belt 
143 |  55 | Massa effect 

我需要返回两种产品搜索(马萨效果和Glutamina黑色查询带)属于相同的商店(store_id)。

在这个例子中,我希望结果是:

id | store_id | name 
----|----------|----------------- 
94 |  9 | Massa effect 
96 |  9 | Glutamina black belt 

编辑

数据的一些例子,我有这个表:

id | store_id | name 
----|----------|----------------- 
    2 |  4 | iPhone 
91 |  9 | Sweet sweat 
94 |  9 | Massa effect 
96 |  9 | Glutamina black belt 
102 |  9 | BCCA 2500 
131 |  1 | Glutamina black belt 
143 |  55 | Massa effect 
147 |  55 | Massa Effect 
200 |  77 | Hey 
202 |  78 | Topster 
206 |  55 | Massa X 
+0

你应该指定你的问题更详细地说。 –

我不是100%肯定,如果这是你想要什么,但你可以尝试这样的:

SELECT id, store_id, name 
FROM ads 
WHERE name regexp '^.*(Massa+.*effect+)|(Glutamina+.*Black+.*Belt+).*$' 
    AND EXISTS(
     SELECT null FROM ads as a WHERE 
     name regexp 
     '^.*(Massa+.*effect+)|(Glutamina+.*Black+.*Belt+).*$' 
     AND a.id != ads.id 
     AND a.store_id = ads.store_id 
); 

我很遗憾的格式。 总的想法是使用“exists”来检查表中是否有另一个具有相似属性的元素,但不是该行本身。如果要确保名称不相同,则可以在where子句中添加另一个条件a.name != ads.name

+2

这是一个很棒的解决方案!当我添加你最后的建议('a.name!= ads。姓名)我完全有我期望的结果。 – Antonello

你可以简单地为您的查询添加另一个约束。

SELECT 
    id, 
    store_id, 
    name 
FROM 
    ads 
WHERE 
    name regexp '^.*(Massa+.*effect+)|(Glutamina+.*Black+.*Belt+).*$' 
    AND store_id = 9; 
+0

不要认为主题首发想要这个。 –

+0

它不会工作,因为store_id可以是。 – Antonello

您可以使用GROUP BY与HAVING COUNT(*)> 1结合使用来查找重复的store_id与正则表达式。

查询

SELECT 
* 
FROM 
ads 
WHERE 
store_id IN (
    SELECT 
    store_id 
    FROM ( 

    SELECT 
     store_id 
    FROM 
     ads 
    WHERE 
     name 
    REGEXP '^.*(Massa+.*effect+)|(Glutamina+.*Black+.*Belt+).*$' 
) 
    ads 
    GROUP BY 
    ads.store_id 
    HAVING COUNT(*) > 1 
) 

结果

| id | store_id |     name | 
|----|----------|----------------------| 
| 94 |  9 |   Massa effect | 
| 96 |  9 | Glutamina black belt | 

演示http://www.sqlfiddle.com/#!9/b3ca8/9

编辑:

使用GROUP_CONCAT得到搜索的ID发了更新,后来他们一起回到了同桌FIND_IN_SET

查询

SELECT 
ads.* 
FROM ( 

    SELECT 
    store_id 
    , GROUP_CONCAT(id) ids 
    FROM ( 

    SELECT 
    * 
    FROM (  
     SELECT 
      id 
     , store_id 
     FROM 
     ads 
     WHERE 
     name 
     REGEXP '^.*(Massa+.*effect+)|(Glutamina+.*Black+.*Belt+).*$' 
    ) 
     ads 
) 
    ads 
    GROUP BY 
    store_id 
    HAVING COUNT(*) > 1 
) 
ads_search 
INNER JOIN 
ads 
ON 
FIND_IN_SET(id, ids) 

结果

| id | store_id |     name | 
|----|----------|----------------------| 
| 94 |  9 |   Massa effect | 
| 96 |  9 | Glutamina black belt | 

演示http://www.sqlfiddle.com/#!9/b3ca8/22

+0

您的解决方案非常棒,但如果表中有其他产品属于商店,则不起作用。商店9(store_id = 9)在此表中有很多其他产品,全部在此查询中返回。 – Antonello

+0

@Antonello尝试更新的查询应该选择更好,因为它有一个更好的过滤器..但因为关闭与FIND_IN_SET JOIN它可以在更大的表上扩展......你能提供更好的示例数据,所以我可以找出一个更好的解决方案,我在那里是anny。 –

+0

我编辑问题并添加一些数据示例 – Antonello