0

My problem details:

logs table

userid offername amount uamount offertitle date

offername are like this

xxx
yyyy
zzzz

My query:

$sqloffers=mysql_query("SELECT user.username, logs.* FROM logs 
INNER JOIN user ON user.userid = logs.userid  ORDER BY logs.offername DESC ");

to display results

<?php

      while($row = mysql_fetch_array($sqloffers))  
{

    echo "<tr> ";
    echo "<td>" .$row[username] . "</td>";
    echo "<td>" .$row[offername] . "</td>";
       echo "<td>" .$row[amount] . "</td>";
    echo "<td>" .$row[uamount] . "</td>";
       echo "<td>" .$row[offertitle] . "</td>";
        echo "<td>" .$row[date] . "</td>";

     }

     echo "</tr> " ;
?>

I want the output to be seperated by each username , aginst offername.

Right now I am getting out like this
username offername amount date
user1 xxxx xxx xxxx
user1 xxxx xxx xxxx
user1 xxxx xxx xxxx
user1 yyyy yyyy yyyy
user1 yyyy yyyy yyyy
user1 yyyy yyyy yyyy

and so on .

I want the output like this

username offername amount date
user1 xxxx xxx xxxx
user1 yyyy yyyy yyyy
user1 zzz zzz zzzz
user2 xxxx xxx xxxx
user2 yyyy yyyy yyyy
user2 zzz zzz zzzz
userN xxxx xxx xxxx
userN yyyy yyyy yyyy
userN zzz zzz zzzz

Clarification:

this table contains offers from different companies completed by users. I need out in such a way that , if user 1 completed 10 offers from offername xxx, and 5 from offername yy, then output should display user1 only 1 time against company xx, and display its count for xxx company, and so on.

I hope i have clarified the question.

Solution that worked after accepting answer:

$sqloffers=mysql_query("
SELECT user.username, logs.offername, logs.date, SUM(logs.uamount) as uamount, SUM(logs.amount) as amount ,count(*) as counter 
FROM logs
INNER JOIN user 
ON user.userid = logs.userid  
GROUP BY user.username
ORDER BY logs.offername DESC
1
  • Welcome to Stackoverflow.To be more clear with question. Commented Feb 13, 2015 at 4:56

2 Answers 2

1

Try this one, just to start:

$sqloffers=mysql_query("
SELECT user.username, logs.offername, logs.date, SUM(logs.uamount) as uamount, SUM(logs.amount) as amount ,count(*) as counter 
FROM logs 
INNER JOIN user 
ON user.userid = logs.userid  
GROUP BY user.username, logs.offername, logs.date
ORDER BY logs.offername DESC 
");
Sign up to request clarification or add additional context in comments.

9 Comments

does not display any records, empty table appears. only the header are shown.
so check my names? I have no idea about your real table structure. does offername belongs to logs table? because my changes do nothing new, there is no filter no WHERE conditions, so that can be only names of fields that break the result
logs table ::::::: userid offername amount uamount offertitle date I only need to pick username from other table rest is same. I have edited the question again with more codes i am using
so I can see 6 fields you try to output, but in your data samles you use only 4 and that is strange. so is offername usually empty? what type is set for this column? try to change offername in my query to offertitle
after change still same, in my question, i displayed sample data.offertitle is empty some times marked as N/A, but offername is always filled up.
|
0
use DISTINCT Modifiers.
DISTINCT  logs.* 

Comments

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.