MYSQL仅列出特定类型的应用

时间:2015-08-28 10:21:56

标签: mysql sql

我有一张这样的表

Username    Published 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

我需要找回仅使用特定类型的已发布应用的用户列表"桌面...

我已设法过滤它们之前的其他查询,因为字符串包含以下值

"%Mencap@Work2-1%"
"%@%"
"%Desktop%"
"%RDP%"

我需要回到以下列表

Username
1stlineWB
aarong
AarthiT
AbbeyY
abbiemc
abbyf
abbygailf
abdio
abdouD
abdulm
AbigailE
AbigailJ
abim
AdamHal

我已尝试过这样的查询

$sql = "SELECT username, publishedapp, count(*) as cnt
from (select username, min(publishedapp) as publishedapp
      from tbl_name t
      group by username
      having count(DISTINCT publishedapp LIKE \"%desktop%\" AND  publishedapp LIKE \"%@%\" AND  publishedapp LIKE \"%RDP%\") = 1
    ) cnt
group by publishedapp
order by count(*) desc
";

但是这会返回那些不包含" desktop"的用户。专门...

2 个答案:

答案 0 :(得分:1)

我怀疑你想要这样的东西: 如果您希望用户只在您的列表中包含一组“已发布”的应用,那么:

select username, group_concat(publishedapp) as apps, count(*) as cnt
from tbl_name t
group by username
having sum(publishedapp NOT LIKE \"%desktop%\" AND
           publishedapp NOT LIKE \"%@%\" AND 
           publishedapp NOT LIKE \"%RDP%\") = 0
order by cnt desc;

这将返回仅拥有这些应用的用户。它还提供了第二列中的应用列表和第三列中的计数。

答案 1 :(得分:0)

这应该是OR条件,而不是

having SUM (CASE WHEN publishedapp LIKE '%desktop%' 
                  OR  publishedapp LIKE '%@%' 
                  OR  publishedapp LIKE '%RDP%' THEN 1 ELSE 0) = 1