组 concat 与条件 2 表
Group concat with 2 tables with conditions
我对这个 MYSQL statement.The 数据的 concat 有问题,如下所示:
> ID: 60abae122046b578881656e1, User: Admin, Server: localhost.local
> ID: 60abae122046b578881656e1, User: Admin, Server: Test Device
> ID: 60aed69a89eb406cd6eaaf89, User: Steve Madden, Server: wpdesktop01
> ID: 60d09c2990d1887919cc8aff, User: Stu Hope, Server: pf-us1-smd
> ID: 60d09c2990d1887919cc8aff, User: Stu Hope, Server: pfv-us1-smd
> ID: 5d826b16827c0805683af8f5, User: test, Server: pf-us1-ansible
> ID: 5d826b16827c0805683af8f5, User: test, Server: pf-us1-dmz-sftp
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-dev
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-ansible
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-zabbix
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-sm
想法是让它看起来像这样:
> ID: 60abae122046b578881656e1, User: Admin, Server: localhost.local, Test Device
> ID: 60aed69a89eb406cd6eaaf89, User: Steve Madden, Server: wpdesktop01
> ID: 60d09c2990d1887919cc8aff, User: Stu Hope, Server: pf-us1-smd, pfv-us1-smd
> ID: 5d826b16827c0805683af8f5, User: test, Server: pf-us1-ansible, pf-us1-dmz-sftp
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-dev, pf-us1-ansible, pf-us1-zabbix, pf-us1-sm
我目前的陈述是这样的:
$sql_modal = 'SELECT
pf_jc_user_audit.jc_id,
pf_jc_user_audit.jc_user,
pf_jc_user_server_ass.system_id,
pf_jc_servers.hostname
FROM
pf_jc_user_audit,
pf_jc_user_server_ass, pf_jc_servers
WHERE
pf_jc_user_audit.jc_id = pf_jc_user_server_ass.jc_id_ass and pf_jc_user_server_ass.system_id = pf_jc_servers.system_id';
我试过修改:
group_concat(pf_jc_servers.hostname)
然后:
group by pf_jc_user_audit.jc_id
但是运气不好,有什么想法吗?
首先,使用带有 ON
子句和表别名的适当连接来缩短代码并使其更具可读性。
然后按jc_id
和jc_user
分组:
SELECT ua.jc_id, ua.jc_user,
GROUP_CONCAT(s.hostname) AS hostname
FROM pf_jc_user_audit ua
INNER JOIN pf_jc_user_server_ass us ON ua.jc_id = us.jc_id_ass
INNER JOIN pf_jc_servers s ON us.system_id = s.system_id
GROUP BY ua.jc_id, ua.jc_user
我对这个 MYSQL statement.The 数据的 concat 有问题,如下所示:
> ID: 60abae122046b578881656e1, User: Admin, Server: localhost.local
> ID: 60abae122046b578881656e1, User: Admin, Server: Test Device
> ID: 60aed69a89eb406cd6eaaf89, User: Steve Madden, Server: wpdesktop01
> ID: 60d09c2990d1887919cc8aff, User: Stu Hope, Server: pf-us1-smd
> ID: 60d09c2990d1887919cc8aff, User: Stu Hope, Server: pfv-us1-smd
> ID: 5d826b16827c0805683af8f5, User: test, Server: pf-us1-ansible
> ID: 5d826b16827c0805683af8f5, User: test, Server: pf-us1-dmz-sftp
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-dev
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-ansible
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-zabbix
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-sm
想法是让它看起来像这样:
> ID: 60abae122046b578881656e1, User: Admin, Server: localhost.local, Test Device
> ID: 60aed69a89eb406cd6eaaf89, User: Steve Madden, Server: wpdesktop01
> ID: 60d09c2990d1887919cc8aff, User: Stu Hope, Server: pf-us1-smd, pfv-us1-smd
> ID: 5d826b16827c0805683af8f5, User: test, Server: pf-us1-ansible, pf-us1-dmz-sftp
> ID: 5c9b585c235ec76b0161c648, User: Rodrigo, Server: pf-us1-dev, pf-us1-ansible, pf-us1-zabbix, pf-us1-sm
我目前的陈述是这样的:
$sql_modal = 'SELECT
pf_jc_user_audit.jc_id,
pf_jc_user_audit.jc_user,
pf_jc_user_server_ass.system_id,
pf_jc_servers.hostname
FROM
pf_jc_user_audit,
pf_jc_user_server_ass, pf_jc_servers
WHERE
pf_jc_user_audit.jc_id = pf_jc_user_server_ass.jc_id_ass and pf_jc_user_server_ass.system_id = pf_jc_servers.system_id';
我试过修改:
group_concat(pf_jc_servers.hostname)
然后:
group by pf_jc_user_audit.jc_id
但是运气不好,有什么想法吗?
首先,使用带有 ON
子句和表别名的适当连接来缩短代码并使其更具可读性。
然后按jc_id
和jc_user
分组:
SELECT ua.jc_id, ua.jc_user,
GROUP_CONCAT(s.hostname) AS hostname
FROM pf_jc_user_audit ua
INNER JOIN pf_jc_user_server_ass us ON ua.jc_id = us.jc_id_ass
INNER JOIN pf_jc_servers s ON us.system_id = s.system_id
GROUP BY ua.jc_id, ua.jc_user