I think you are probably looking for something like this:
SELECT *
FROM
test
WHERE
id=(SELECT max(id) FROM test WHERE name LIKE "somename")
this will return the row from test that has the maximum ID where name is like "somename"
Question
Good day! i've got some problem with that query:
SELECT `id`
FROM `test`
WHERE `name`
LIKE "somename" AND `id` = (SELECT max(`id`) FROM test)
It returned an empty result.
But
SELECT `id`
FROM `test`
WHERE `name` LIKE "somename"
works.
Moreover,
SELECT `id`
FROM `test`
WHERE `id` = (SELECT max(`id`) FROM test)
works too.
Why they doesn't work together?
Thanks you!
Solution
I think you are probably looking for something like this:
SELECT *
FROM
test
WHERE
id=(SELECT max(id) FROM test WHERE name LIKE "somename")
this will return the row from test that has the maximum ID where name is like "somename"
OTHER TIPS
Because they would have to be both true at the same time.
Imagine the following
id name
1 'somename'
2 'someothername'
In the above case
SELECT `id`
FROM `test`
WHERE `name` LIKE "somename"
Will return the first row but:
SELECT id FROM test WHERE id = (SELECT max(id) FROM test)
Will return the second. Using AND
will mean no results are returned. You may wish to use OR
depending on what behaviour you want.
try that:
SELECT `id`
FROM `test`
WHERE id in (SELECT max(id) FROM test WHERE name LIKE '%somename%')