Jump to content

php/mysql help


c_pattle

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.