jackrabb1t Posted July 12, 2010 Share Posted July 12, 2010 I need help to solve a problem I'm having in MySQL. I have two tables: Table 1 id (primary key) name Table 2 id (primary key) name1 (foreign id from table 1) name2 (foreign id from table 2) address phone What I need to do is select all the records in table 2, and order them by the first name from table 1 and then the second name from table 1. Obviously, sorting by the ID's won't work. What is the best way to solve this problem? Quote Link to comment https://forums.phpfreaks.com/topic/207521-help-with-selecting-values-for-two-columns-from-another-table/ Share on other sites More sharing options...
jackrabb1t Posted July 12, 2010 Author Share Posted July 12, 2010 I made a mistake in the post above: name2 (foreign id from table2) should be : name2 (foreign id from table1) Because name2 is a name from table1 just like name1 is. Quote Link to comment https://forums.phpfreaks.com/topic/207521-help-with-selecting-values-for-two-columns-from-another-table/#findComment-1084948 Share on other sites More sharing options...
Alura Posted July 21, 2010 Share Posted July 21, 2010 I'm working on a similar issue, and am trying to use a JOIN clause for my page. Here's an example from W3Schools: SELECT column_name(s) FROM table_name1 JOIN table_name2 ON table_name1.column_name=table_name2.column_name http://w3schools.com/sql/sql_join.asp I'm having issues with getting my JOIN to work, but from what you've said, it looks like you might need to see about using that and then SORT by code to get what you want. Sorry this wasn't much help - hope you're able to get it to work. Quote Link to comment https://forums.phpfreaks.com/topic/207521-help-with-selecting-values-for-two-columns-from-another-table/#findComment-1089166 Share on other sites More sharing options...
samona Posted July 21, 2010 Share Posted July 21, 2010 Try the following: SELECT column_name(s) FROM table_name1, table_name2 ON table_name1.column_name=table_name2.column_name Quote Link to comment https://forums.phpfreaks.com/topic/207521-help-with-selecting-values-for-two-columns-from-another-table/#findComment-1089205 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.