Jump to content

Recommended Posts

The title is a bit misleading, but it's basically what I want to do. Let me explain, but before I do so the version of MySQL that I want to use this query on is 5.0.67.

 

I think what I want to do is best explained by showing an example.

 


-----------------------------------------------------
| table1                                            |
-----------------------------------------------------
| t1id(int)(p key) | name(string) | active(boolean) |
-----------------------------------------------------
| 1                | name1        | 1               |
-----------------------------------------------------
| 2                | name2        | 0               |
-----------------------------------------------------
| 3                | name3        | 1               |
-----------------------------------------------------

-----------------------------------------------------------------------------------
| table2                                                                          |
-----------------------------------------------------------------------------------
| t2id(bigint)(p key) | t1id(int) | time(int)  | value(double) | current(boolean) |
-----------------------------------------------------------------------------------
| 1                   | 1         | 1328848200 | 33.5          | 0                |
-----------------------------------------------------------------------------------
| 2                   | 2         | 1328848201 | 44.2          | 0                |
-----------------------------------------------------------------------------------
| 3                   | 3         | 1328848202 | 23.5          | 0                |
-----------------------------------------------------------------------------------
| 4                   | 3         | 1328848210 | 10.7          | 0                |
-----------------------------------------------------------------------------------
| 5                   | 2         | 1328848212 | 15.3          | 1 (may be 0)     |
-----------------------------------------------------------------------------------
| 6                   | 1         | 1328848215 | 33.9          | 0                |
-----------------------------------------------------------------------------------
| 7                   | 3         | 1328848230 | 17.4          | 0                |
-----------------------------------------------------------------------------------
| 8                   | 1         | 1328848245 | 12.1          | 0                |
-----------------------------------------------------------------------------------
| 9                   | 1         | 1328848246 | 15.7          | 1                |
-----------------------------------------------------------------------------------
| 10                  | 3         | 1328848255 | 12.2          | 0                |
-----------------------------------------------------------------------------------
| 11                  | 3         | 1328848270 | 22.5          | 1                |
-----------------------------------------------------------------------------------

-----------------------------
| result                    |
-----------------------------
| t1id | time       | value |
-----------------------------
| 1    | 1328848245 | 15.7  |
-----------------------------
| 3    | 1328848255 | 12.2  |
-----------------------------

 

To simplify it a little bit:

  1. [*]Filter every row in table2 that has the same t1id as a table1 where in table1 the row has active=false.

[*]Filter every row in table2 that has current=true in table2.

[*]Filter every row in table2 where another row has the same t1id and a higher time value.

[*]SELECT t1id, time, value. Very important that t1id and value is from the same row as time

 

I understand I could probably have put it all in the same table, but I don't want to do that, and I'm still left with the problem that I'm wondering about. I know I have to use some sub-query, and it would probably be some INNER JOIN, but I'm not exactly sure how to write it.

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/256823-selecting-the-row-after-group-by/
Share on other sites

Because I did a typo and can't edit it anymore, I must post a new post...

 

EDIT:


-----------------------------
| result                    |
-----------------------------
| t1id | time       | value |
-----------------------------
| 1    | 1328848245 | 12.1  |
-----------------------------
| 3    | 1328848255 | 12.2  |
-----------------------------

 

As you can see, I did a typo in the result table, which would really confuse anyone trying to understand this.

I thought I posted it... hmm, might have been lost when the site went down for a little while?

 

SELECT t1id, time, value FROM table1 LEFT JOIN table2 ON table1.t1id = table2.t1id WHERE active = true AND current = false ORDER BY time DESC LIMIT 1

 

Now this would get me more than one row with the same t1id. I could potentially do a workaround in PHP while I fetch the data into arrays, but it might become very heavy if there is a lot of entries in table 2. Is there any way to remove the "duplicate" rows, kinda GROUP BY them, but without using GROUP BY, because GROUP BY wouldn't give me the row with the highest time value in table2?

Hi

 

I am not 100% sure what you are trying to get, as you seem to be saying to filter for something, then your SQL filters them out if that is the case.

 

However to get the latest item you need a subselect for that to JOIN on as well. Also as you need matching rows on both tables you can use an INNER JOIN rather than an OUTER JOIN.

 

Something like this, although you will have to check it is really what you want:-

 

SELECT table1.t1id, table2.time, table2.value 
FROM table1 
INNER JOIN table2 
ON table1.t1id = table2.t1id AND table1.active = true
INNER JOIN (SELECT t1id, MAX(time) AS MaxTime FROM table2 WHERE current = false GROUP BY t1id) SubQuery
ON table2.t1id = SubQuery.t1id AND table2.time = SubQuery.MaxTime

 

All the best

 

Keith

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.