Djrikidk Posted June 21, 2010 Share Posted June 21, 2010 I'm not sure if this is actually supposed to be in some sort of a SQL forum instead, but since i don't know any and it is very close to PHP i'll just write it here. I wanna know how i can order by a column from another table, because i have two tables, and my PHP code is meant to show users from Table 1, which is used to store login information and such, but it should Order the values by a column from a different table, Table 2, which stores the amount of money and strength the users got. Every user has a row in both tables, with a column with their Username in it. Link to comment https://forums.phpfreaks.com/topic/205429-order-by-column-from-different-table/ Share on other sites More sharing options...
gwolgamott Posted June 21, 2010 Share Posted June 21, 2010 What are you trying to order it by then and why? You should just order it by the key you use to link the tables... otherwise when you get the data from the table via php then you can use php to order it whatever manner you want I suppose. Link to comment https://forums.phpfreaks.com/topic/205429-order-by-column-from-different-table/#findComment-1075040 Share on other sites More sharing options...
Djrikidk Posted June 21, 2010 Author Share Posted June 21, 2010 What are you trying to order it by then and why? You should just order it by the key you use to link the tables... otherwise when you get the data from the table via php then you can use php to order it whatever manner you want I suppose. I have the two tables, Table 1 and Table 2, my PHP code displays data from every row of Table 1, the data displayed is currently ordered by a column from Table 1, but i want it to be ordered by a column from Table 2, where it is the same rows except that in Table 2 it displays player stats like money, strength and such. So i would like that when the values from Table 1 is echoed out, they're sorted by a the strength column which is in Table 2. Link to comment https://forums.phpfreaks.com/topic/205429-order-by-column-from-different-table/#findComment-1075046 Share on other sites More sharing options...
Mchl Posted June 21, 2010 Share Posted June 21, 2010 Something like this SELECT t1.username, t1.someOtherField, t2.aFieldFromTable2 FROM table1 AS t CROSS JOIN table2 AS t USING(username) ORDER BY t2.aFieldFromTable2 Link to comment https://forums.phpfreaks.com/topic/205429-order-by-column-from-different-table/#findComment-1075052 Share on other sites More sharing options...
gwolgamott Posted June 21, 2010 Share Posted June 21, 2010 Okay I guess I misunderstood what you wanted, but Mchl solution should work perfectly for you now that I see what you mean. Link to comment https://forums.phpfreaks.com/topic/205429-order-by-column-from-different-table/#findComment-1075059 Share on other sites More sharing options...
Djrikidk Posted June 21, 2010 Author Share Posted June 21, 2010 Thanks alot! I'll see if it works with my code! Link to comment https://forums.phpfreaks.com/topic/205429-order-by-column-from-different-table/#findComment-1075230 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.