0

Must need coffee or something :/ Cant figure this out.

I have a table with following info:

[B]
unique_id  block_id   column_id     order_id   user_id[/B]
1          block-1    column-2      0          1
32         block-2    column-3      0          7
7          block-1    column-1      0          2
8          block-2    column-2      0          2
10         block-6    column-2      1          1
11         block-7    column-3      1          1

I'm trying to find the highest block_id numer that the user_id 1 has.

Thought I could use something similar to this:

SELECT * FROM `blocks` WHERE unique_id=(select max(unique_id) from blocks) and user_id= 1

But it returns empty.
In this case I would want to get "block-7"

Hope anyone can help :/

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Zedith
0

Your brackets are wrong.
Your sub-select returns the number 32, and the outer select cannot find any row with user_id = 1 and unique_id = 32.
Try:

select * from blocks order by unique_id desc where user_id=1 limit 1;

or

select * from blocks where user_id=1 and unique_id=(SELECT max(unique_id) from blocks where user_id=1);
0

i think you said you want to find the highest block_id

then why using max(unique_id)

True debasisdas.
Thats a mistake I made. I am trying to get the highest block_id from each user.
So unique_id should not be relevant.
But when I try to use the max(block_id) instead I get only a block_id from the middle of the table. :/

Edited by Zedith: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.