Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FrancescoZaffaroni's avatar

SQL - How to get specific resut in GROUP BY

Hi,

I have a table that is like this id value new uuid 1 Hello 0 AAAA 2 Hi there 0 AAAB 3 HelloWorld 1 AAAA

and i'd like to have a query that gets every result that is new = 1 and all the retults with new = 0 if there is not an new = 1 with the same uuid, like this id value new uuid 2 Hi there 0 AAAB 3 HelloWorld 1 AAAA

i have tried with GROUP BY but it displays

id value new uuid 1 Hello 0 AAAA 2 Hi there 0 AAAB

is it possible to have this avoiding sub queries?

Thank you all!

edit: since tables are not working ( @JeffreyWay ) here is a screenshot http://s18.postimg.org/gyfe1i0jd/Cattweura.png

0 likes
14 replies
FrancescoZaffaroni's avatar

Wow, i had tried this but didn't work and now it does, thank you so much. And then I can flip the array with php.

edit: just kidding doesn't work

to expand, if i put it after the order by it doesn't change the result, if i put it before it throws an error.

psmail's avatar

I'm not certain your question is clear enough to get you the answer you are looking for - you might want to provide more / clearer information.

FrancescoZaffaroni's avatar

Yeah, sorry, but english is not my first language. I'll try to be clearer.

In my table i have a set of rows. When I edit a record i want, instead of update, to create another row with the value 'new' equal to 1, and the same uuid.

Then when i retrive everything I want to get all the "new" data and when isn't available the new data, get the original one.

It's like database versioning but I only care about the last and the first versions, so each edited record will have 2 rows.

I figured out i can retrive the new values (when available) and the old ones with a

SELECT * FROM foobars GROUP BY uuid;

But the result I get is the one from the third example table I posted ( http://s18.postimg.org/gyfe1i0jd/Cattweura.png ), the new values get hidden by GROUP BY, instead i want the original data to get hidden when newer is available, like the second table in the example.

I hope i explained what I mean, sorry again.

henrique's avatar

I can't see how this would be possible with pure SQL and avoiding subqueries. You would need to select those where new = 1, then UNION it with those where new = 0 and subquery where don't have new = 1

FrancescoZaffaroni's avatar

I was wondering if was possible to decide the logic which the GROUP BY function uses. It would be pretty easy with 2 queries and some php.

psmail's avatar

Mmm ... still not 100% sure of the question, but here goes -

When updating a record, you don't want to overwrite the original. Instead, you want to append (or add) a record, flagging the most recent update (or added row) as being the active, valid record.

If I have understood correctly, I don't think you are going about it the right way. Doing it your way, @henrique is right and straight SQL won't get you there.

But I think I have done something like this before. Instead of using a 'new' column use timestamps and use a correlated subquery in your select statement to only select the most recently added (or update ... the vernacular is a little confusing) records ... that is, the related records with the maximum timedate stamp. That way, you can use straight SQL.

FrancescoZaffaroni's avatar

Yeah, you got it, but after the first, any further modification will update the one with the new=1, so i don't really care about timestamps since i will always have one original and one edited.

Thank you for all the replies :)

psmail's avatar

Yes. But you will need to build logic to create the new=1 and then change everything else to new=0. Using timestamps, you don't have to do any of that.

But if you are content with that, all the best.

chrio's avatar

Something along these lines will work like you described:

select * from (select * from test
order by new desc) as foo
group by uuid
order by id

You can experiment with it at sqlfiddle.com

FrancescoZaffaroni's avatar

Yeah @psmail you are probably right! I'll try your way and see which i like the best. I could also split in 2 tables.

Please or to participate in this conversation.