GarethB Posted May 25, 2011 Share Posted May 25, 2011 Hi guys, This one has really confused me. The Tables: users (id, fname, lname, teamname, won, lost, total) teams (id, teamname, divison, league) Variables $division="Division 2"; $league="Thursday"; I need to create a query that will do the following in a while loop (the bits in brackets is the bits I can't figure out): SELECT * FROM users WHERE teamname=(teamname from table teams, division = division from teams and league = leagues from teams) ORDER BY won DESC, total ASC I hope that makes sense but I really can't work it out! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/237443-complex-select-query-using-two-tables/ Share on other sites More sharing options...
GarethB Posted May 25, 2011 Author Share Posted May 25, 2011 Would this be right? $players = "SELECT * FROM users WHERE teamname=(SELECT teamname FROM teams WHERE division='$division' AND league='$league') ORDER BY won DESC, total ASC"; Quote Link to comment https://forums.phpfreaks.com/topic/237443-complex-select-query-using-two-tables/#findComment-1220120 Share on other sites More sharing options...
mikosiko Posted May 25, 2011 Share Posted May 25, 2011 this is better SELECT users.* FROM users JOIN teams ON users.teamname = teams.teamname AND teams.division='$division' AND teams.league='$league' ORDER BY users.won DESC, users.total ASC however, I will suggest you to review your tables design; the data modeling could be improved... per. example (just a couple of small ones): - in your table user you should be referencing/storing the team.id and not the team.teamname (same concept could be applied to the fields division and league in your table teams). - in your table user you should no be storing a dependent field (total field is a dependent one)... calculated values should be calculated upon retrieval. Quote Link to comment https://forums.phpfreaks.com/topic/237443-complex-select-query-using-two-tables/#findComment-1220131 Share on other sites More sharing options...
GarethB Posted May 25, 2011 Author Share Posted May 25, 2011 Thank you - that works great. I have started using the id's of certain things but not for the team. Would it be best changing it now before it gets too late? I presume this is for quicker referencing? As for the total thing, when the results go into the table, it simply increments total by however many have been played. Is there a reason why this is incorrect? Thanks for your time - I'm still quite new and abosirbing as much as possible! Quote Link to comment https://forums.phpfreaks.com/topic/237443-complex-select-query-using-two-tables/#findComment-1220142 Share on other sites More sharing options...
mikosiko Posted May 25, 2011 Share Posted May 25, 2011 I have started using the id's of certain things but not for the team. Would it be best changing it now before it gets too late? I presume this is for quicker referencing? You should if you want to have a good DB design, remember that the relational model is the foundation of SQL, therefore the better your model the better and easy your queries will be (Did I sound like Yoda here ?)... and no.. is not for quick referencing (but could result on).. is for proper db modeling. As for the total thing, when the results go into the table, it simply increments total by however many have been played. Is there a reason why this is incorrect? that is easily covered for any aggregation function (COUNT, SUM, etc) on retrieval... in a good DB model you should avoid store dependent fields in your table. Maybe a search and read upon "Database Normalization" will help you further Quote Link to comment https://forums.phpfreaks.com/topic/237443-complex-select-query-using-two-tables/#findComment-1220147 Share on other sites More sharing options...
GarethB Posted May 26, 2011 Author Share Posted May 26, 2011 Thank you very much for your time and help (Yoda! ). I will have a good read and will improve upon my current knowledge. Quote Link to comment https://forums.phpfreaks.com/topic/237443-complex-select-query-using-two-tables/#findComment-1220337 Share on other sites More sharing options...
GarethB Posted May 26, 2011 Author Share Posted May 26, 2011 I've had a good read on Database Normalization. Would I be alright staring a new thread to discuss the database structure? Quote Link to comment https://forums.phpfreaks.com/topic/237443-complex-select-query-using-two-tables/#findComment-1220552 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.