3

Alright I have the MYSQL query code setup to pull this information out of my database. What I would like to do now is count by code but group it by name.

$sql = "SELECT  m.* 
          FROM (SELECT DISTINCT Code  
                  FROM Transaction) md 
          JOIN Transaction m ON m.ID_Transaction = (SELECT  ID_Transaction
                                                      FROM Transaction mi
                                                     WHERE mi.Code = md.Code 
                                                       AND Date_Time=CURdate() 
                                                       AND Time_Stamp!=''
                                                  ORDER BY m.Name DESC, mi.Code DESC, mi.Date_Time DESC, mi.ID_Transaction DESC
                                                     LIMIT 1)";

With my php code this is what i get from this query.

rick, 002455, 2010-11-10, 08:30 AM  
rick, 003819, 2010-11-10, 08:45 AM  
amber, 003572, 2010-11-10, 08:45 AM  
eric, 001479, 2010-11-10, 10:30 AM  
jerry, 001012, 2010-11-10, 09:45 AM   

rick being name
000000 being code

I want to take this information and count it like so for each name.

rick = 2  
amber = 1  
eric = 1  
jerry = 1  

I'm not sure how I would do this? Can I use the information in the query to make another query to count this information.

1 Answer 1

2
SELECT m.name, count(*) FROM (
SELECT DISTINCT Code
FROM Transaction
) md JOIN Transaction m ON
m.ID_Transaction = ( SELECT ID_Transaction FROM Transaction mi WHERE mi.Code = md.Code AND Date_Time=CURdate() AND Time_Stamp!='' ORDER BY m.Name DESC, mi.Code DESC, mi.Date_Time DESC, mi.ID_Transaction DESC LIMIT 1 )
 group by m.name
Sign up to request clarification or add additional context in comments.

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.