jim.davidson Posted June 12, 2007 Share Posted June 12, 2007 I'm using mySQL 4.1.21 php and dreamweaver 8 I have two tables, I need to get a recordset that has all the records from one table and one record from the other. How do I query it so that I get the state_id and state_name from Table B for a individual customer_id and all the state_id's and state_names from Table A (Table A sorted on country ascending) Table A Table B state_id int(10) state_id int(10) state_name varchar(50) state_name varchar(50) country varchar(50) customer_id int(10) Example: 401 Hemshire (from Table B) 001 Alabama (the rest from Table A) 002 Alaska 003 Arkansas Quote Link to comment https://forums.phpfreaks.com/topic/55269-solved-would-like-help-with-a-query/ Share on other sites More sharing options...
Wildbug Posted June 12, 2007 Share Posted June 12, 2007 (Please use code tags, especially when posting monospaced text like above.) SELECT state_id,state_name FROM table_b WHERE customer_id=??? UNION SELECT state_id,state_name FROM table_a Quote Link to comment https://forums.phpfreaks.com/topic/55269-solved-would-like-help-with-a-query/#findComment-273175 Share on other sites More sharing options...
jim.davidson Posted June 12, 2007 Author Share Posted June 12, 2007 Thanks, that helps some...but I still need to have table A ordered by country and state_name. I tried this and get an error...Unknown table 'states' in order clause SELECT state_id,state_name FROM other_states WHERE customer_id=46 UNION SELECT state_id,state_name FROM states ORDER BY states.country, states.state_name Quote Link to comment https://forums.phpfreaks.com/topic/55269-solved-would-like-help-with-a-query/#findComment-273193 Share on other sites More sharing options...
Illusion Posted June 12, 2007 Share Posted June 12, 2007 No need to specify in ORDER BY clause as states.country, states.state_name, simply write as ORDER BY country, state_name Quote Link to comment https://forums.phpfreaks.com/topic/55269-solved-would-like-help-with-a-query/#findComment-273208 Share on other sites More sharing options...
jim.davidson Posted June 12, 2007 Author Share Posted June 12, 2007 Thanks for the help this works SELECT state_id, state_name, country FROM other_states WHERE customer_id =46 UNION SELECT state_id, state_name, country FROM states ORDER BY country, state_name Quote Link to comment https://forums.phpfreaks.com/topic/55269-solved-would-like-help-with-a-query/#findComment-273217 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.