2017-10-04 98 views
0

我有這個疑問:過濾器的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

你應該指定你的問題更詳細地說。 –

回答

2

我不是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

0

你可以簡單地爲您的查詢添加另一個約束。

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

不要認爲主題首發想要這個。 –

+0

它不會工作,因爲store_id可以是。 – Antonello

1

您可以使用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