TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-21-2009, 08:05 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default mysql JOIN challange

I have spent the last week trying to create a query that returns the data from one table in the format i want it but without success. I am sure I need to JOIN a table with itself but its the COUNTing of types of row which is proving most challenging. Wonder if anyone can help or even further resolve for me. Its a challange indeed.

Essentially I want a query that will take this table:

Code:
betid 	        customer 	group 	result
34		joebloggs	1	Win
34		another		1	Lose
36		mrjones		1	Win
37		joebloggs	1	Win
38		mrJones		1	Lose
38		joebloggs	1	Lose
40		joebloggs	2	Win
and reformat it in this way counting up wins and losses GROUPing by group, customer and then win and loss counts.

Code:
group		customer	Wins	Losses
1		joebloggs	2	1
1		mrjones		1	1
1		another		0	1
2		joebloggs	1	0
Handling 0 losses is not easy as I am essentially trying to return a count of value for a row that does not exist.
captainmerton is offline  
Reply With Quote
Old 07-21-2009, 08:36 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Use 0 and 1 instead of "win" and "loose". Numbers are better for machines to parse. I would also use IDs for customers instead of names, attach a related table if you want info on them. I also don't see how a join statement is needed for what you have, join is for adding results from associated tables.

I wrote this off the top of my head, this should be something close to what you need.
Code:
SELECT
    b.group, 
    b.customer, 
    (SELECT COUNT(betid) WHERE result="win" AND customer=b.customer) as wins, 
    (SELECT COUNT(betid) WHERE result="lose" AND customer=b.customer) as losses 
FROM 
    bets b
GROUP BY 
    b.customer
ORDER BY 
    b.group ASC
I'm not fully sure how to do the grouping here, I don't have the time to play with it because I am at work.
__________________

Village Idiot is offline  
Reply With Quote
Old 07-21-2009, 08:51 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Building onto Village Idiot's example:

sql Code:
SELECT
    `customer` AS `customer_id`, `group`, `customer`,
    (SELECT COUNT(`id`) FROM `results` WHERE `result` = 'Win' AND `customer` = `customer_id`) AS `wins`,
    (SELECT COUNT(`id`) FROM `results` WHERE `result` = 'Lose' AND `customer` = `customer_id`) AS `losses`
FROM
    `results`
GROUP BY
    `customer`
ORDER BY
    `wins`
DESC
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 07-21-2009, 09:33 PM   #4 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

That is pretty much what i'm looking for thanks. Glad this was in the absolute beginners section as that is what i obviously am. Only issue i have is its returning th extra customer_id column which matches customer but its an extra column i dont really want to return. Any way i could adjust the query to return the 4 columns instead of the 5?
captainmerton is offline  
Reply With Quote
Old 07-21-2009, 09:50 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Naturally!

sql Code:
SELECT
    `group`, `customer`,
    (SELECT COUNT(`id`) FROM `results` WHERE `result` = 'Win' AND `customer` = `r`.`customer`) AS `wins`,
    (SELECT COUNT(`id`) FROM `results` WHERE `result` = 'Lose' AND `customer` = `r`.`customer`) AS `losses`
FROM
    `results` `r`
GROUP BY
    `customer`
ORDER BY
    `wins`
DESC
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
captainmerton (07-22-2009)
Old 07-22-2009, 03:56 PM   #6 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Perfect! Much appreciated.
captainmerton is offline  
Reply With Quote
Old 07-23-2009, 12:09 PM   #7 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Quote:
Use 0 and 1 instead of "win" and "loose". Numbers are better for machines to parse. I would also use IDs for customers instead of names, attach a related table if you want info on them.
Thanks for all your help guys but i wanted to pick up on these earlier comments. I think going with 0 and 1 for wins and losses makes sense. As for using ids instead of character usernames in my table - will this make a big difference. i thought about this at design stage but decided against it as i didnt really need ids as all usernames would be unique and assumed they would just be stored as hex or some kind of binary number value anyway and stored in the db in the most efficent way. Can someone explain whether going with numeric ids and using a userdetail table to get the actual character name would be better as in essence i would be reading 2 tables instead of 1 everytime i wanted a username. But will change if it is more efficient to go with numeric ids. Any thoughts?
captainmerton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL join total delayedinsanity MySQL & Databases 3 07-24-2008 03:53 AM
MySQL syntax for table join on delete delayedinsanity General 5 04-03-2008 05:39 PM
MySQL Sell Up Alan @ CIT The Lounge 12 01-17-2008 05:46 PM
Mysql problem, most likely related to left join Village Idiot MySQL & Databases 6 01-04-2008 06:09 AM
Notepage like application to open large MySQL files Wildhoney General 6 12-07-2007 02:18 PM


All times are GMT. The time now is 12:55 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design