1

I want to know how to remove duplicate values from the output of an sql query.

This is the sql query:

$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";

This is the output of that query:

{"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"2000"},  
{"Username":"absc868","TariffName":"s1","ElecConsump":"1900"}]}

As you can see, the query filters out data where the data matches todays date. We have 2 outputs for the same user. The value of the tariff name and username are the same,but the energy consumption value is different which is fine.

I want to achieve the following output:

= {"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"2000 +1900"}


= {"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"3900"}

Could someone point me to the direction in how I can achieve this?

Thank you in advance to those who read the post and contributed!

5
  • Do you want to sum the values or you want them as 2000 +1900 ? Commented Nov 6, 2016 at 16:04
  • @Dekel Hi, yes I want the sum of the values Commented Nov 6, 2016 at 16:11
  • 1
    So the answer by @scaisEdge is what you are looking for. Commented Nov 6, 2016 at 16:11
  • @Dekel thanks Your comment is loyal .. i upvote your answer .. (the meaning of the answer is practically the same) Commented Nov 6, 2016 at 16:14
  • well, not exactly the same because he wanted a sum (and not a string [like in his example]). you got my vote here as well :) Commented Nov 6, 2016 at 16:15

2 Answers 2

2

You can use the group_concat function:

$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";
Sign up to request clarification or add additional context in comments.

Comments

1

You should use a sum and a group by

$query = "SELECT useraccount.Username as Username, tariff.Name as TariffName, 
sum(energyconsumption.ElecEnergy) as ElecConsump
    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 as tariffs";

(you have some difference between table column name alias and object attribute name )

8 Comments

Brilliant! Thank you so much! Could i ask you one last question. I tested the query, and im getting an error. This is the rest of the code after the query: $result = mysqli_query($conn,$query); $r = array(); if($result->num_rows){ while($row = mysqli_fetch_array($result)){ array_push($r, array( 'Username' => $row['Username'], 'TariffName' => $row['tariffs'], 'ElecConsump' => $row['ElecEnergy'] )); } } echo json_encode(array('results' => $r)); The error is in this line: if($result->num_rows)
@Dekel will do. Thanks!
@scaisEdge Done. Thank you once again for your help!
@Mahbs . if you have another question post a proper new question .. so all the SO user can help you
@scaisEdge Will do! Thank you once again!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.