查询同一个表中的特定名称(MySQL)

时间:2022-09-18 13:13:43

Table devices:

Name: Router1
Type: NODE
Class: Main

Name: Router1
Type: IF
Class: T1 0/0/1

Name: Router1
Type: IF
Class: Fa 0/1

The above is a minimal example.

以上是一个最小的例子。

The output I desire is:

我想要的输出是:

Router1 | T1 0/0/1, Fa 0/1

Is this possible without creating temp tables, procedure, or a script?

这是否可以在不创建临时表,过程或脚本的情况下实现?

I've thought deeply, but in the real world example there are multiple devices I need their IFs class from. You cannot just pull each device in any way I know.

我深思熟虑,但在现实世界的例子中,有多个设备我需要它们的IF类。你不能以我知道的任何方式拉动每个设备。

1 个解决方案

#1


You can use GROUP_CONCAT on class column while group them according to name

您可以在类列上使用GROUP_CONCAT,同时根据名称对它们进行分组

SELECT Name, GROUP_CONCAT(class)
FROM devices
GROUP BY Name
WHERE Type= 'IF'
/*WHERE Name = 'Router_1'*/

#1


You can use GROUP_CONCAT on class column while group them according to name

您可以在类列上使用GROUP_CONCAT,同时根据名称对它们进行分组

SELECT Name, GROUP_CONCAT(class)
FROM devices
GROUP BY Name
WHERE Type= 'IF'
/*WHERE Name = 'Router_1'*/