不包含的用户;筛选和列出如果多个... MYSQL

时间:2015-08-25 14:58:47

标签: mysql sql

我有这张桌子;

User         app
1stLineS     Citrix Delivery Services Console
1stLineS     Mencap@Work2-1
1stLineS     PB Service Desktop
1stlineWB    PB Service Desktop
aabidam  Business Object Full Client
aabidam  Epicor
aabidam  Mencap Desktop with Acrobat
aabidam  Powerplan
aabidam  Therefore Navigator
aabidam  Thin Client Desktop
aarong   Mencap @ Work Desktop
AarthiT  Mencap@Work2-1
AbbeyY   Mencap@Work2-1
abbiemc  Mencap@Work2-1
abbyf    Mencap@Work2-1
abbygailf    Mencap@Work2-1
Abbyh    GLH Contractors Web Portal
abdio    
abdio    PB Service Desktop
abdio    RDP NC-BACKUP01
abdouD   Mencap@Work2-1
abdulm   Mencap@Work2-1
AbigailE     Mencap@Work2-1
AbigailJ     Mencap Desktop with Acrobat
AbigailJ     Mencap@Work2-1
abim     Mencap@Work2-1
abiodunb     Mencap@Work2-1
abiodunb     Powerplan
adamd    ActiveH
adamd    Mencap@Work2-1
AdamHal  Mencap@Work2-1
adamhe   Mencap Desktop with Acrobat
adamhe   Mencap@Work2-1
adamhe   Powerplan
AdamK    Mencap@Work2-1
AdamL    Epicor
AdamL    Mencap@Work2-1
AdamL    Powerplan
AdamL    Saturn Live
AdamL    Therefore Navigator
AdamL    Therefore Viewer
adamr    ActiveH
adamr    ALMSdotNET
adamr    Epicor
adamr    FirstClass
adamr    Mencap@Work2-1
adamr    Peterborough Desktop
adamr    Peterborough Desktop with Acrobat
adamr    Saturn Live
adamr    Saturn MS ACCESS ADD-ONS
adamr    Therefore Navigator
adamr    Therefore Viewer
AdamTh   Mencap Desktop with Office 2010
AdamTh   Mencap@Work2-1
AdamTh   Microsoft Excel
AdamTh   OfficeBase Live
AdamTh   Peterborough Desktop with Acrobat
AdamTh   ResourceLink-Aurora
AdamTh   Thin Client Desktop
Adelec   Mencap Desktop with Office 2010
AdeleC   Mencap@Work2-1
AdeolaA  Mencap@Work2-1
adetolaok    Mencap@Work2-1

我需要回来

aabidam

因为它是唯一拥有多个应用的​​用户,并且没有" Mencap @ work2-1"

我正在使用;

WHERE publishedapp LIKE \"%Mencap@Work2-1%\"
OR publishedapp NOT LIKE \"%@%\"
AND publishedapp NOT LIKE \"%Desktop%\"
AND publishedapp NOT LIKE \"%RDP%\"

过滤桌面,但我不认为这是我想要的正确的程序方式。

最终我喜欢做类似

的事情
GROUP_CONCAT(DISTINCT publishedapp SEPARATOR \"<br>\") AS publishedapp

列出应用

我认为我必须以某种方式使用HAVING,COUNT和SUM,但我还没能破解它。

我做oposite,那就是那些拥有&#34; Mencap @ Work2-1&#34;至少有一个这个

的应用程序
SELECT DISTINCT username,
GROUP_CONCAT(DISTINCT publishedapp SEPARATOR \"<br>\") AS publishedapp
FROM tbl_name
WHERE publishedapp LIKE \"%Mencap@Work2-1%\"
OR publishedapp NOT LIKE \"%@%\"
AND publishedapp NOT LIKE \"%Desktop%\"
AND publishedapp NOT LIKE \"%RDP%\"
GROUP BY username
HAVING COUNT(DISTINCT publishedapp) > 1 AND
SUM(DISTINCT publishedapp LIKE \"%Mencap@Work2-1%\") > 0

1 个答案:

答案 0 :(得分:1)

所以,没有拥有'Mencap @ Work2-1'应用的用户。 其他人,只要他们有两个不是'桌面','RDP','@'的应用程序

获取拥有其中一个不需要的应用的所有用户,并将其从总计中移除:

SELECT DISTINCT username FROM tbl_name
WHERE username NOT IN 
    ( SELECT DISTINCT username FROM tbl_name WHERE  publishedapp  LIKE "%Mencap@Work2-1%" )
AND publishedapp NOT LIKE "%@%" AND publishedapp NOT LIKE "%Desktop%" AND publishedapp NOT LIKE "%RDP%"
GROUP BY username
HAVING COUNT(*) > 2