我根据公共ID映射了两个表,但我想将所有与特定公司相关联的电话号码放在一行而不是多行,分为三列“电话”,“传真”和“免费电话”。下面是数据输出的示例。
Dr. Dimes 5553456879 Telephone
Dr. Dimes 5553455600 Toll Free
Dr. Dimes 5553450123 Fax
SELECT
fleet.company_name,
phone_number.phone_number,
phone_type.name
FROM
fleetseek.phone_number,
fleetseek.phone_type,
fleetseek.fleet,
fleetseek.fleet_phone
WHERE
phone_number.phone_type_id = phone_type.phone_type_id AND
fleet.fleet_id = fleet_phone.fleet_id AND
fleet_phone.phone_number_id = phone_number.phone_number_id
答案 0 :(得分:4)
执行此操作的最简单方法可能是使用聚合:
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
reporter.start(10, TimeUnit.SECONDS);
答案 1 :(得分:0)
当您使用同一个表三次时,您可以通过连接和别名来完成获取一行中的所有列:
SELECT
fleet.company_name,
telephone.phone_number AS Telephone,
toll_free.phone_number AS Toll_Free,
fax.phone_number AS Fax
FROM
fleetseek.fleet
JOIN
fleet_phone
ON
fleet.fleet_id = fleet_phone.fleet_id
LEFT OUTER JOIN (SELECT
phone_number
FROM
fleetseek.phone_number,
fleetseek.phone_type
WHERE
phone_number.phone_type_id = phone_type.phone_type_id AND
phone_type.name = 'Telephone') telephone
ON
fleet_phone.phone_number_id = telephone.phone_number_id
LEFT OUTER JOIN
(SELECT
phone_number
FROM
fleetseek.phone_number,
fleetseek.phone_type
WHERE
phone_number.phone_type_id = phone_type.phone_type_id AND
phone_type.name = 'Toll Free') toll_free
ON
fleet_phone.phone_number_id = toll_free.phone_number_id
LEFT OUTER JOIN
(SELECT
phone_number
FROM
fleetseek.phone_number,
fleetseek.phone_type
WHERE
phone_number.phone_type_id = phone_type.phone_type_id AND
phone_type.name = 'Fax') fax
ON
fleet_phone.phone_number_id = fax.phone_number_id;
一般设计选择建议:如果信息非常重要,请将您的手机类型与电话号码放在同一个表格中。必须添加连接会影响性能(同时影响空间)。通常,性能比空间更昂贵。