Jump to content

Display last result of current data


mycow

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.