mycow Posted January 19, 2009 Share Posted January 19, 2009 I have a database with 2 tables. The one table contains number1 and status. The second table contains number1 info1 info2. I am trying to find a way to output those data in the order of table1.number1 table1.status table2.info1 table2.info2 but the problem is that i want to output only the last of info1. number1 in the second table has for example 10 different records with different results in info1 and i only want the last one. these data (number1) are numbers that are ascending as the data grows. Sorry if i didn't make it that clear. Quote Link to comment Share on other sites More sharing options...
dropfaith Posted January 19, 2009 Share Posted January 19, 2009 im not sure i understand your issue order by Id desc limit 1 would order them by id and only select the highest numbered Id Quote Link to comment Share on other sites More sharing options...
mycow Posted January 19, 2009 Author Share Posted January 19, 2009 Let me give you an example. 1st row number1 10 status ON 2nd row number1 20 status ON 3rd row number1 21 status OFF and the second table number1 10 info1 001 info2 002 2nd row number1 10 info1 002 info2 000 3rd row number1 20 info1 0056 info2 000asd And so on. And i want to echo allo the number1s with the last info1 number. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2009 Share Posted January 19, 2009 It would have been MUCH better to use some real-world names/data instead of table1, table2, etc. For instance you could have said you had a situation similar to a customer table and an order table and you were looking to get the details for the last order for each customer. Anyway, this might work for you, give it a try: SELECT table1.number1, table1.status, MAX(table2.info1), table2.info2 FROM table2 JOIN table1 on table1.number1 = table2.number2 GROUP BY table2.number1 Reference: http://www.tizag.com/mysqlTutorial/mysqlmax.php Quote Link to comment Share on other sites More sharing options...
mycow Posted January 19, 2009 Author Share Posted January 19, 2009 Ok lets say i have the following for table1 ID is a unique number User is a username LID is a number (not unique) and the second table has the following aa0 unique number aa1 another unique number LID same as the table1 So i want to have the following table1.LID table1.ID table2.aa0 table2.aa1 where table1.LID = table2.LID but i want only the last table2.aa0 and that for all the table1.ID in the table1 Sorry for the mess Quote Link to comment Share on other sites More sharing options...
trq Posted January 19, 2009 Share Posted January 19, 2009 Which part of the examples provided are you not understanding? Quote Link to comment Share on other sites More sharing options...
mycow Posted January 19, 2009 Author Share Posted January 19, 2009 The examples only provide me with one record and nothing else. that's my problem. Quote Link to comment Share on other sites More sharing options...
fenway Posted January 19, 2009 Share Posted January 19, 2009 The examples only provide me with one record and nothing else. that's my problem. And what do you want? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2009 Share Posted January 19, 2009 Your table description is either wrong or it just won't work. You state that the LID field is what links table1 and table2 data and that there are many records in table2 that link to records in table1. But, you also state that LID is not unique in table1 (the user table). If LID is not unique in table1 how do you know which records in table2 link to which records in table1? I did have a typo in the code I posted previusly - but I did link to the page with an explanation. You could have read that page and figured it out for yourself. Assuming that LID is unique in table 1 (but not table2) than this should work for you: (same as I provided above, but with different field names) SELECT table1.ID, table1.LID, MAX(table2.aa0) aa0, table2.aa1 FROM table2 JOIN table1 on table1.LID = table2.LID GROUP BY table2.LID Quote Link to comment 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.