TEENFRONT Posted October 10, 2006 Share Posted October 10, 2006 How do work with and retreive data from 2 tables ( how to i retrieve, order, etc ) any tuts about? Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/ Share on other sites More sharing options...
obsidian Posted October 10, 2006 Share Posted October 10, 2006 [quote author=TEENFRONT link=topic=111119.msg450114#msg450114 date=1160506717]How do work with and retreive data from 2 tables ( how to i retrieve, order, etc ) any tuts about?[/quote]do the tables relate in any way, or is it just two random tables? :D Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107085 Share on other sites More sharing options...
TEENFRONT Posted October 11, 2006 Author Share Posted October 11, 2006 both tables have identical columns :) Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107316 Share on other sites More sharing options...
alpine Posted October 11, 2006 Share Posted October 11, 2006 UNION[code]<?phpmysql_query("(select * from table WHERE field = '$what') UNION ALL (select * from table WHERE field = '$what') order by field desc");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107331 Share on other sites More sharing options...
TEENFRONT Posted October 11, 2006 Author Share Posted October 11, 2006 ooooooooooooohil try it straight away... il let you know my progress.but heres another question first.... needs some explaining..i have the 2 tables... they have colums "played" and "won" among others.i want to order it by the players rating, BUT i dont work out the rating untill i have got the played and won values. get my point?so...simply put..sql query pulls username, played, won.i then figure out the players rating ( a ratio of the 2 values) it returns 1,2,3,4 or 5.i THEN want to show only the top 10 players with a 5 rating.. how on earth do i do that?if you need me to explain a little further, i will :) Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107343 Share on other sites More sharing options...
HuggieBear Posted October 11, 2006 Share Posted October 11, 2006 It's possible, and ideally you'd be better processing it in MySQL and then using an ORDER BY clause.Can you provide the method of working out the rating based on ratio?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107371 Share on other sites More sharing options...
TEENFRONT Posted October 11, 2006 Author Share Posted October 11, 2006 yes sure...2secs. i use a custom functionfunction get_rating($played, $won){ if($played<10){ return 0; } $p = round($won/($played/5)); if($p>5){ $p=5; } if($p<=0){ $p=0; } return $p;}$rating = get_rating($played, $won);echo $rating Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107384 Share on other sites More sharing options...
TEENFRONT Posted October 11, 2006 Author Share Posted October 11, 2006 any further help HuggieBear ? Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107504 Share on other sites More sharing options...
HuggieBear Posted October 11, 2006 Share Posted October 11, 2006 I've edited this post as I made a stupid commen t, as for the solution of letting MySQL do the work for you, akitchin has shown you how to do that belowRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107675 Share on other sites More sharing options...
akitchin Posted October 11, 2006 Share Posted October 11, 2006 alright, this sounds like a job for the CASE operator. it works much like a switch structure:[code]SELECT table1.something AS something, table2.played AS played, table2.won AS won, ROUND((5*won)/played) AS calculated_rating, CASE WHEN played < 10 THEN 0 WHEN calculated_rating > 5 THEN 5 ELSE calculated rating END AS actual_ratingFROM table1, table2WHERE table1.linking_column=table2.linking_columnORDER BY actual_rating DESC, table1.something_else ASC[/code]some MySQL gurus out there might optimize it, but that SHOULD do what you're after. it seems a little daunting, but hopefully the spacing and linebreaks help make it clearer. look up the manual entry for CASE structures (in the functions and operators chapter i think), and give that a whirl.i should note that i've simplified the "actual" rating determination a little bit. it should reduce anyone with less than 10 games under their belt to 0, and should simplify anyone with a rating greater than 5 to 5. however, the rating will never be negative (unless you've got a negative played or won number, which you theoretically shouldn't, unless they REALLY suck) so there's no need to have the <= 0 clause in there. every other rating (between 0 and 5) is kosher to send back directly. Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-107686 Share on other sites More sharing options...
TEENFRONT Posted October 12, 2006 Author Share Posted October 12, 2006 holy crap, now thats a query lol.Il look up the CASE info and il get back to you guys if i get stuck lol. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-108086 Share on other sites More sharing options...
TEENFRONT Posted October 12, 2006 Author Share Posted October 12, 2006 iv just thought... maybe theres an easier way...ok, so columns username, played and won in table1, table2 and table3 ( if you look up you see i need to pull info from multiple tables..table1,table2,table3 all 3 tables have username,played and won columns)i want to displayed top 10 players. so if username has played 700 and won 700, hes the best. so hes number 1. if username2 has played 650 and won 649, hes number 2 etc. i can forget the rating.How do i do a query that works that out for me? Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-108093 Share on other sites More sharing options...
akitchin Posted October 13, 2006 Share Posted October 13, 2006 as far as i can tell, you'd want to do something like this:[code]SELECT table.username AS username, table.played AS played, table.won AS won, ROUND(won / played, 2) AS win_ratio FROM tables_used WHERE stuff ORDER BY win_ratio DESC, played DESC, username ASC[/code]this will grab all users with the highest win ratio first, and in the case of matching win ratios, will choose the one with the highest number of games played first (since i assume seniority ranks). then it'll sort by username. this may be oversimplified since it's easy to have a 1.00 win ratio when you're 1 for 1, so you may want to weight it by the number of games played somehow - nothing comes to me off the top of my head (i haven't done stats for a bit), but i'm sure a helpful statistician will pop up soon enough.[b]EDIT: after some thought, unless you normalize all your ratios (that is, turn it all into a gaussian), i think the only way to ensure a relatively fair comparison of win ratios is to add a WHERE clause that makes sure the users have played more than x games.[/b] Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-108168 Share on other sites More sharing options...
ridiculous Posted October 13, 2006 Share Posted October 13, 2006 From a learner's standpoint, this is a great thread. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/23586-one-for-ye-gurus/#findComment-108188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.