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!
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";
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 )
Related
I have this query in my php and it seems to be fetching the wrong data set from my db.
$querystring = "
SELECT a.*,
b.itemcolour,
b.itemcolourname
FROM itemorders AS a
INNER JOIN catalogueitemscolour AS b
ON a.colourid = b.colourid
WHERE a.colourid IN(SELECT colourid
FROM itemorders
WHERE orderid = 61)
";
Here is a picture of my results
Can I know why it's not selecting the specific orderID of 61?
You can try below -
SELECT a.*, b.itemColour,b.itemColourName FROM itemorders
AS a INNER JOIN CatalogueItemsColour AS b ON a.colourID = b.colourID WHERE
a.orderID = 61
Because you are not putting condition on orderID, rather putting it on colorID. What you actually want is this condition: WHERE a.orderID = 61.
I have tables:
trips: Trip_Num, Trip_Type,Trip_Geographic, etc.
travelers_trips_history: Username, Trip_Num, Trip_Type, Trip_Season, etc.
In table 2 there are trips the user did and I need to know what is the most common Trip_Geographic that in his trip's history (The Trip_Geographic need to be known by table 1 - trips by his Trip_Num).
I am doing a project in PHP and need to do a SQL query for a specific user (by his username) - need to receive a table that the columns are: 1. Trip_Geographic, count of the occurrence the Trip_Geographic is in the travelers_trips_history.
Attached the tables I have:
trips
travelers_trips_history
The query I did:
$query = "select traveler_trips_history.*, COUNT(trips.Trip_Geographic) AS Trip_Geographic_occurrence
FROM traveler_trips_history INNER JOIN trips ON
travelers_trips_history.Trip_Num = trips.Trip_Num
GROUP BY Trip_Geographic
ORDER BY Trip_Geographic_occurrence DESC
WHERE traveler_trips_history.Traveler_Username = $username";
I receive a error about the Group BY (check syntax), however, I am not sure the query is going to do what it should do.
Try this
SELECT traveler_trips_history.*
,COUNT(trips.Trip_Geographic) AS Trip_Geographic_occurrence
FROM traveler_trips_history
INNER JOIN trips ON travelers_trips_history.Trip_Num = trips.Trip_Num
WHERE traveler_trips_history.Traveler_Username = $USERNAME
GROUP BY traveler_trips_history.travelers_trips_history
,traveler_trips_history.Username
,traveler_trips_history.Trip_Num
,traveler_trips_history.Trip_Type
,traveler_trips_history.Trip_Season
ORDER BY Trip_Geographic_occurrence DESC
You have 2 issues in your query:
1- You should have the where clause before the GroupBy Clause
2- If you GroupBy Col1, you can only select Col1 or use Aggregate Functions on another Columns (SUM, AVG, etc...)
You need to check the Rules for GroupBy and understand it better.
Edit after your comment:
$query = "select Trip_Geographic, COUNT(*) AS Trip_Geographic_occurrence
FROM traveler_trips_history INNER JOIN trips ON
travelers_trips_history.Trip_Num = trips.Trip_Num
WHERE traveler_trips_history.Traveler_Username = $username
GROUP BY Trip_Geographic
ORDER BY Trip_Geographic_occurrence DESC";
Try like this
The problem is your where clause and group by columns.
SELECT tr.Trip_Geographic
,COUNT(hs.*) AS Trip_Geographic_occurrence
FROM traveler_trips_history hs
INNER JOIN trips tr ON hs.Trip_Num = tr.Trip_Num
WHERE hs.Username = $USERNAME
GROUP BY tr.Trip_Geographic
ORDER BY Trip_Geographic_occurrence DESC
Your problem is Haing WHERE clause after GROUP By and using columns that are not in GROUPED BY or AGGREGATE functions.
Try the following which has the username, trip_geographic and the no of trips.
select traveler_trips_history.Traveler_Username, traveler_trips_history.Trip_Geographic, COUNT(trips.Trip_Geographic) AS Trip_Geographic_occurrence
FROM traveler_trips_history INNER JOIN trips ON
travelers_trips_history.Trip_Num = trips.Trip_Num
GROUP BY Trip_Geographic, traveler_trips_history.Traveler_Username
having traveler_trips_history.Traveler_Username = $username
ORDER BY Trip_Geographic_occurrence DESC
i need help getting data from different tables and insert into other different table Here are the Queries
"SELECT commentID, date, comment, subject, parentID, aBUserID FROM comments WHERE status = 'APPROVED'"
"SELECT topicID, subForumID, aBUserID, lastPostID, views, replies, startDate FROM topic WHERE status = 'APPROVED' AND topicID = $parentid";
// $parentID need to be matched from above query parentID,
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID";
// $cmtaBUserID = aBUserID from first query
"SELECT userName FROM users WHERE aBUserID = $topicaBUserID";
//$topicaBUserID = aBUserID from second query
Last 2 queries are from same table but using different where clause
i used different inner join left join from solutions posted here but non of these worked for me stuck since last 2 weeks please help
PS data from all above Queries will be inserted to a single table i need these to be combined so i can have them all in one place
If you want to perform the operation in same query use 'OR'
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID OR aBUserID = $topicaBUserID";
Please try this
SELECT userName from users where aBUserID IN(SELECT aBUserID FROM comments WHERE status = 'APPROVED')
Couldn't test it but Maybe this is what you are looking for.
SELECT c.commentID, c.date, c.comment, c.subject, c.parentID, c.aBUserID,
t.topicID, t.subForumID, t.aBUserID, t.lastPostID, t.views, t.replies, t.startDate,
u.userName
FROM
comments c
left outer join topic t on t.topicID = c.parentID
left outer join users u on u.aBUserID = c.aBUserID and u.aBUserID = t.aBUserID
WHERE
c.status = 'APPROVED' and t.status = 'APPROVED';
try this:
SELECT
comment.[commentID],
comment.[date],
comment.[comment],
comment.[subject],
comment.[parentID],
comment.[aBUserID],
commentuser.[userName],
topic.[topicID],
topic.[subForumID],
topic.[aBUserID],
topic.[lastPostID],
topic.[views],
topic.[replies],
topic.[startDate],
topic.[userName]
FROM comments comment
LEFT OUTER JOIN users commentuser
ON commentuser.aBUserID = comment.[aBUserID]
LEFT OUTER JOIN
(
SELECT
t.[topicID],
t.[subForumID],
t.[aBUserID],
t.[lastPostID],
t.[views],
t.[replies],
t.[startDate],
u2.[userName] --user from users table joined to topics table
FROM topic t
LEFT OUTER JOIN users u
ON u.aBUserID = t.[aBUserID]
WHERE t.[status] = 'APPROVED'
) topic
ON topic.topicID = comment.parentID
WHERE comment.[status] = 'APPROVED'
Suppose I have two table
Table Name group
column 1: giver
column 2: acceptor
Table name userinfo
column 1: name
column 2: status
i want to select giver,acceptor and userinfo.status that from group table where giver or acceptor whose name is zakir that giver or acceptor exist in uerinfo table as name.
Need Help to write sql statement for taht query..
Thanks in advance... :)
What you are referring to is also known as the INNER JOIN clause in an SQL Statement.
Depending on the relationship you can create an INNER JOIN to potentially connect the two variables that are identical.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Extracted from W3Schools.com
Try the below query,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp JOIN userinfo as ui
on ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Or try this without join,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp, userinfo as ui
WHERE ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Try this:
SELECT `group`.`giver`, `group`.`acceptor`, `userinfo`.`status`
FROM `group`, `userinfo`
WHERE (`group`.`giver` = 'zakir' OR `group`.`acceptor` = 'zakir')
AND `userinfo`.`name` = 'zakir'
select g.giver, g.acceptor u.status
from group g, userinfo u
where u.name = 'zakir'
and (g.giver = u.name or g.acceptor = u.name)
It should do a part of the job.
This assumes you want the status for both the giver and the acceptor
May need a little tweaking for mysql syntax
Select giver, g.status, acceptor, a.status
FROM GROUP
join userinfo as g on group.giver = g.name
join userinfo as a on group.acceptor = a.name
where (giver = 'zakir' or acceptor = 'zakir')
I want to order the result based on another query. Here's my query:
"SELECT * FROM red_table INNER JOIN blue_table ON red_table.ID = blue_table.ID";
I'd like to add ORDER BY using the result from another query:
"SELECT date FROM blue_table WHERE status = 'ready'";
So the final will be something like:
"SELECT * FROM red_table INNER JOIN blue_table ON red_table.ID = blue_table.ID ORDERY BY ( SELECT date FROM blue_table WHERE status = 'ready' )";
How do I do that? Or is it possible?
UPDATE:
I know I could just use ORDER BY blue_table.date but the date column contains values that are not Date. blue_table is kind of a metadata table. So I need to match the values from the date column with the status column so I only get the date values and use that for ordering. Maybe I shouldn't have called it column date but I hope you get my point.
Maybe you mean something like this? You would have to add additional column names in select
SELECT status, date
FROM red_table INNER JOIN blue_table ON red_table.ID = blue_table.ID
GROUP BY 1, 2
SELECT *
FROM red_table
INNER JOIN blue_table ON red_table.ID = blue_table.ID
WHERE blue_table.status = 'ready'
ORDER BY blue_table.date
Note that if blue_table does not contain a match for the join condition, nothing from red_table will be shown.