![](/img/trans.png)
[英]How to create MYSQL query to get specific results (count all unique values from one column)
[英]MySQL - get all unique values of a column, check if has a specific value
首先-對可憐的頭銜表示歉意,我不知道如何用一種語言來形容它。 我有一張桌子-片段在下面。
mysql> select * from playlistfiles;
+-----------------------+--------------+-----------+
| FileName | PlaylistName | FileIndex |
+-----------------------+--------------+-----------+
| File1 | Image1 | 0 |
| File1 | Video1 | 2 |
| File2 | Video1 | 0 |
| File3 | Video1 | 1 |
| File4 | Image1 | 1 |
| File4 | Video1 | 3 |
+-----------------------+--------------+-----------+
6 rows in set (0.00 sec)
我需要做的是獲取所有FileName,以及文件是否在播放列表中,並按FileIndex進行排序,即對於Image1播放列表,輸出應為
+-----------------------+------------+-----------+
| FileName | InPlaylist | FileIndex |
+-----------------------+------------+-----------+
| File1 | 1 | 0 |
| File2 | 0 | Null |
| File3 | 0 | Null |
| File4 | 1 | 1 |
+-----------------------+------------+-----------+
而Video1將是
+-----------------------+------------+-----------+
| FileName | InPlaylist | FileIndex |
+-----------------------+------------+-----------+
| File2 | 1 | 0 |
| File3 | 1 | 1 |
| File1 | 1 | 2 |
| File4 | 1 | 3 |
+-----------------------+------------+-----------+
簡而言之,我需要能夠從表中獲取所有唯一的FileName,並檢查它是否在給定的表中,如果是,則按FileIndex對其進行排序。
我將從這樣的事情開始,然后從那里開始:
select plf.fileName,
max(if(plf2.fileName = plf.fileName, 1, 0)) as inPlayList,
max(plf2.FileIndex)
from playlistfiles plf
left join playlistfiles plf2
on plf.fileName = plf2.fileName
and plf2.playListName = @playListName
group by plf.fileName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.