views:

456

answers:

3

Hi everyone, i want something like this:

SELECT DISTINCT ID from copies WHERE timestamp < 1229444506 ORDER BY CID

the problem is that this one only return ID and i need CID and other columns in the table.

It may be that this is totally wrong for other reason aswell, so i will explain what i need.

I have a table that "record" every row-change in my maintable. This so in the future i will be able to go back in time and see how the main table looked like a certain date in time.

So what i need is a query that ORDER all rows BY CID, WHERE timestamp < 1229444506, AND then DISCTINCT them by ID.

I want to the query to return the first row by ID, when ordered by CID newest first.

Ive tried to get this working with different methods but no luck. Any suggestions?

A: 
SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID ORDER BY CID DESC;

EDIT:

SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID ORDER BY CID;

You can also try:

SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID ASC ORDER BY CID;

Or:

SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID DESC ORDER BY CID;

Does it work?

Alix Axel
Hi eyze, this do not work. I've tried this and it do not sort CID correctly. Why it does that i do not know.
Filip Palm
I'm sorry, my mistake. I'll edit my answer.
Alix Axel
No i have already tried different sorting methods with this query and no luck. Thats whats weird, but i do not know the inner workings of MySQL so there is certainly a good explanation to why it behaves like this.
Filip Palm
A: 

how can your database know which rows to append to your distinct column? say you haveː

 id | name
 ---+-----
  1 | a
  1 | b

now the hypothetical query: SELECT DISTINCT id, name FROM table. which name do you expect to show up in your result? a or b?

if that's not a problem you can group your result in mysql by id, but it’s unspecified which name you'll get

SELECT `id`, `name` FROM `copies` WHERE `timestamp` > 123456789 GROUP BY `id` ORDER BY `cid` DESC
knittl
Hi knitti, thats what CID is for. Its the UNIQUE value in the table and i want the lates CID WHERE timestamp < 1229444506.So the answer to your question A or B, it depends on which have the highest CID AND if both are timestamp < 1229444506.
Filip Palm
then go with Ondrej Palkovsky’s solution, it’s exactly what you want
knittl
+2  A: 

I am not sure if I got it correctly, but what about creating a subquery that will just select the columns? You wrote: "I want to the query to return the first row by ID, when ordered by CID newest first."

So, let's make a subquery:

SELECT id, max(cid) as maxcid FROM copies WHERE timestamp < XX group by id

This will give you the relationship id <=> the CID you want. And now join it:

SELECT copies.* FROM copies, (SELECT id, max(cid) as maxcid FROM copies WHERE timestamp < xxx group by id) x  
WHERE copies.id=x.id AND copies.cid=x.maxcid;
ondra
This seems to work if i want the absolute lates row, but if i want timestamp < 1229444506 where should i put timestamp < 1229444506?
Filip Palm
Inside the subquery.
ondra
At a first glance this seems to do the trick, ill going to run some checks and see if its okay.Thank you Ondrej Palkovsky.
Filip Palm