我想知道如何从sql查询的输出中删除重复值。
这是sql查询:
$query = "SELECT useraccount.Username, tariff.Name as tariffs,
energyconsumption.ElecEnergy, useraccount.Username as User
FROM useraccount
INNER JOIN tariff
ON useraccount.tariffs = tariff.id
INNER JOIN energyconsumption
ON energyconsumption.User = useraccount.id
WHERE Date = CURRENT_DATE";
这是该查询的输出:
{"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"2000"},
{"Username":"absc868","TariffName":"s1","ElecConsump":"1900"}]}
如您所见,查询过滤掉数据与当前日期匹配的数据。我们为同一个用户提供了2个输出。关税名称和用户名的价值是相同的,但能耗值是不同的,这很好。
我想实现以下输出:
= {"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"2000 +1900"}
= {"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"3900"}
有人能指出我如何实现这个目标吗?
提前感谢那些阅读帖子并做出贡献的人!
答案 0 :(得分:2)
您可以使用group_concat
功能:
$query = "SELECT useraccount.Username, tariff.Name as tariffs,
GROUP_CONCAT(energyconsumption.ElecEnergy SEPARATOR ' +')
FROM useraccount
INNER JOIN tariff
ON useraccount.tariffs = tariff.id
INNER JOIN energyconsumption
ON energyconsumption.User = useraccount.id
WHERE Date = CURRENT_DATE
GROUP BY useraccount.Username, tariff.Name";
答案 1 :(得分:1)
你应该使用一个总和和一个小组
@EnableAutoConfiguration
(表列名别名和对象属性名称之间有一些区别)