我有一个包含多个共享路径的表我希望得到这些路径中前N个不同的Servernames的所有路径,例如这里的整个表格。
+----+--------------------------------+
| ID | BACKUPPATH |
+----+--------------------------------+
| 1 | //server.domain/share/folder |
| 2 | //server.domain/share/folder3 |
| 3 | //server.domain/share/folder2 |
| 4 | //server2.domain/share/folder1 |
| 5 | //server2.domain/share/folder2 |
| 6 | //server3.domain/share/folder1 |
| 7 | //server3.domain/share/folder2 |
| 8 | //server3.domain/share/folder3 |
+----+--------------------------------+
每次Servernames都会有所不同,不同Servenames的数量也会有所不同。作为示例,我想获得前两个不同的Servernames的所有路径,我期望作为结果:
+----+--------------------------------+
| ID | BACKUPPATH |
+----+--------------------------------+
| 1 | //server.domain/share/folder |
| 2 | //server.domain/share/folder3 |
| 3 | //server.domain/share/folder2 |
| 4 | //server2.domain/share/folder1 |
| 5 | //server2.domain/share/folder2 |
+----+--------------------------------+
作为子查询我使用以下查询来获取Servernames的行集:
select SUBSTRING_INDEX(BACKUPPATH,'/',3) as SERVERNAMES from(select BACKUPPATH from Backuppaths GROUP BY SUBSTRING_INDEX(BACKUPPATH,'/',3))as NUMEROFSERVERS LIMIT 2;
+------------------+
| SERVERNAMES |
+------------------+
| //server.domain |
| //server2.domain |
+------------------+
我现在被困在如何使用这个子查询来获得我期望的结果。
感谢您提供的任何帮助
答案 0 :(得分:1)
您可以加入内联视图:
select s.*
from servernames s
join (select substring_index(backuppath, '/', 3) as servername
from servernames
group by servername
order by min(id) limit 2) v
on substring_index(backuppath, '/', 3) = v.servername
order by id
答案 1 :(得分:0)
使用Distinct
SELECT DISTINCT SUBSTRING_INDEX(BACKUPPATH,'/',3) AS SERVERNAMES FROM Backuppaths LIMIT 2;