[英]MySQL - get all unique values of a column, check if has a specific value
First off - apologies for the poor title, I have no idea how to describe it in a one-liner. 首先-对可怜的头衔表示歉意,我不知道如何用一种语言来形容它。 I have a table - snippet is below.
我有一张桌子-片段在下面。
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)
What I need to do is to get all the FileNames and whether the file is in a playlist or not, as well as order them by FileIndex ie for the Image1 playlist, the output should be 我需要做的是获取所有FileName,以及文件是否在播放列表中,并按FileIndex进行排序,即对于Image1播放列表,输出应为
+-----------------------+------------+-----------+
| FileName | InPlaylist | FileIndex |
+-----------------------+------------+-----------+
| File1 | 1 | 0 |
| File2 | 0 | Null |
| File3 | 0 | Null |
| File4 | 1 | 1 |
+-----------------------+------------+-----------+
and Video1 would be 而Video1将是
+-----------------------+------------+-----------+
| FileName | InPlaylist | FileIndex |
+-----------------------+------------+-----------+
| File2 | 1 | 0 |
| File3 | 1 | 1 |
| File1 | 1 | 2 |
| File4 | 1 | 3 |
+-----------------------+------------+-----------+
In short, I need to be able to get all the unique FileNames from the table, and check if it is in a given table and if so, order it by FileIndex. 简而言之,我需要能够从表中获取所有唯一的FileName,并检查它是否在给定的表中,如果是,则按FileIndex对其进行排序。
I would start with something like this and play from there: 我将从这样的事情开始,然后从那里开始:
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.