c_pattle Posted January 19, 2011 Share Posted January 19, 2011 I want to do a mysql query that takes the last two records from two tables and combines them into one row. So for example if I have a table called "table1" and the latest records are id - name 35 - chris 36 - chris2 37 - chris3 and a table called "table2" where the latest records are id - fruit 16 - banana 17 - apple 18 - pear How can I do a query that will merge the last two rows into one so the result set would be, id - name / id - fruit 37 - chris3 / 18 - pear 36 - chris2 / 17 - apple Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/225020-phpmysql-help/ Share on other sites More sharing options...
Coolkat Posted January 19, 2011 Share Posted January 19, 2011 SELECT table1.id, table1.name, table2.id, table2.fruit FROM table1 LEFT JOIN table2 ORDER BY table1.id, table2.id DESC LIMIT 2; I think? I have never been great at SQL JOIN queries, but something along those lines would be what you need. a JOIN of some sort. Here is a description of the SQL JOIN keywords: http://www.w3schools.com/sql/sql_join.asp Link to comment https://forums.phpfreaks.com/topic/225020-phpmysql-help/#findComment-1162211 Share on other sites More sharing options...
c_pattle Posted January 19, 2011 Author Share Posted January 19, 2011 Thanks. I tried this but I'm not sure it's going to work. A left join will list all the records from the table on the left even if there is no match with the records from the table on the right. Because there are no matching elements in either of the tables it means I get the records from the table on the left and just "NULL" from all of the records from the table on the right. Link to comment https://forums.phpfreaks.com/topic/225020-phpmysql-help/#findComment-1162225 Share on other sites More sharing options...
c_pattle Posted January 19, 2011 Author Share Posted January 19, 2011 Is there a way to create a temporary field which will assign an number to each record on both tables, then I could simply compare those two numbers...? Link to comment https://forums.phpfreaks.com/topic/225020-phpmysql-help/#findComment-1162234 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.