SQL using count value as offset - mysql

This is a follow on from another question i made
I have this query that counts all the records up to a certain point i choose whcih works fine
SELECT count(*)FROM news WHERE id < 18
this query gives me a count of 7
I am now having problems with the offset which would be the result of the above query
I tried using this query
SELECT * FROM `news` ORDER BY id DESC LIMIT 7,1
but i get id number 13 instead of 18
The ids i should have is 2, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 22, 23
I have tried using order by id desc in the count query which does give a different result but still wrong id displayed

I dont see a problem here: You order the result by id DESC which means your result is ordered by other way around and 8th value(0..7) is 13.
Try sorting it by ASC then it will give you 18

Related

Trying to SELECT everything but excluding between IDs doesnt work

I'm trying to SELECT everything but not between the 6 id and 12 id but it doesn't work it only returns everything
Here is my code:
SELECT nom FROM genre WHERE id_genre BETWEEN id_genre=!6 AND id_genre=!12;
Thanks.
When you write condtition WHERE id BETWEEN 6 AND 12, what you want is to find those ids that are greater or equal than 6 and less or equal than 12, i.e. id >= 6 AND id <= 12.
Now, you want to negate it, so you'd write (accordingly to mathematical logic rules): id < 6 OR id > 12, which means you want ids outside this range.
Or, you could write simply: NOT (id BETWEEN 6 AND 12).

ORDER BY FIELD not working - error message

I have this Select:
SELECT pts_name
FROM products_tecspecs
WHERE pts_id IN ( 5275, 21, 5276, 5277,
5278, 49, 5279 )
ORDER BY FIELD (pts_id, 5275, 21, 5276, 5277, 5278, 49, 5279)
I am trying to get the result and order it the way I want. I found this answer but got this error: #1305 - FUNCTION database_name.FIELD does not exist
I am trying to get specifics results with IN (that's working), but sorting the way I want.
Help you be apreciated.
Remove the space between the function and the parenthesis: FIELD (
MySQL parser does not like spaces there. Use:
FIELD(pts_id, ...)
you can also try subquery to order by records in custom order
SELECT pts_name
FROM (SELECT pts_name,
CASE pts_id
WHEN 5275 THEN 1
WHEN 21 THEN 2
WHEN 5276 THEN 3
WHEN 5277 THEN 4
WHEN 5278 THEN 5
WHEN 49 THEN 6
WHEN 5279 THEN 7
END AS sort_order
FROM products_tecspecs
WHERE pts_id IN ( 5275, 21, 5276, 5277, 5278, 49, 5279)
) a
ORDER BY a.sort_order ASC ;

Order items in MySQL by a fixed list?

I am going to have a fixed list of items to order by that I won't know until I run the query since there is a randomization step.
I would like to have something like the following:
Assume that is_launch_set will return 1, 3, 7, 11 but have been randomized to below:
SELECT * FROM items WHERE is_launch_set=1 ORDER BY id values (3,11,7,1);
Any ideas on how to achieve this? I was thinking maybe a find_in_set but not really sure.
You can do that by using either:
ORDER BY FIND_IN_SET(id, '3,11,7,1')
or
ORDER BY FIELD(id, 3, 11, 7, 1)
or
ORDER BY CASE id WHEN 3 THEN 0
WHEN 11 THEN 1
WHEN 7 THEN 2
WHEN 1 THEN 3
ELSE 4
END
In mys sql 8.0 it order only by the first column - 3 in this case:
ORDER BY FIND_IN_SET(id, '3,11,7,1')
With this example it worked, but upside. you'll see the 3 last, then 11, then 7 etc.
ORDER BY FIELD(id, 3, 11, 7, 1)
If you chose this example, you'll see the 1 first, then 7, then 11 etc.
ORDER BY FIELD(id, 3, 11, 7, 1) DESC

SQL - select first in value set by priority?

I'm fairly inexperienced in SQL and this seems like it must be an easy task, but I'm not sure how to go about it.
Basically I want to select a single row from table A where field "someField" is in a pre-determined set "someSet", but I want it to look for each value in the set individually. For example, let's say "someSet" contains 5, 6, 9, 3. I would use a query similar to this:
SELECT * FROM A WHERE someField IN (5, 6, 9, 3) LIMIT 1
However, I want it to look for 5 first, then 6, then 9, then finally 3 if no rows have been found yet. Written as separate queries it'd look like this:
SELECT * FROM A WHERE someField = 5 LIMIT 1
(if no results returned)
SELECT * FROM A WHERE someField = 6 LIMIT 1
(if no results returned)
SELECT * FROM A WHERE someField = 9 LIMIT 1
(if no results returned)
SELECT * FROM A WHERE someField = 3 LIMIT 1
Obviously using 4 queries (theoretically infinite queries) isn't very elegant, is there a way to make this into a single query?
You can do
SELECT * FROM A WHERE someField IN (5, 6, 9, 3)
ORDER BY FIELD( someField, 5, 6, 9, 3)
LIMIT 1

Get result from mysql orderd by IN clause

I have the following query
SELECT * FROM invoice WHERE invoice_id IN (13, 15, 9, 27)
My result is:
invoice_id | invoice_number | ...
------------------------------------
9 | 201006003 |
13 | 201006020 |
15 | 201006022 |
27 | 201006035 |
which is the result set I want except that is ordered by the invoice_id (which is an autoincrement value).
Now I want the result in the order I specified in my query (13, 15, ...). Is there a way to achive that?
The background is that I have a DataTable bound to a DataGridView. The user can filter and sort the result but if he want's to print the result I don't use the DataTable for printing because it only contains the most important columns and instead I pull the whole records from the database and pass it to my printing control.
I also tried to extend the existing DataTable with the missing results but that seems to slower than using the IN (...) query.
It's ugly, but you could do:
ORDER BY CASE invoice_id WHEN 13 THEN 0 WHEN 15 THEN 1 WHEN 9 THEN 2 WHEN 27 THEN 3 ELSE 4 END
Actually, there's the FIELD function:
ORDER BY FIELD(invoice_id, 13, 15, 9, 27)
The FIELD function returns the position of the first argument in the list of the rest.
Or, if you're generating it dynamically, you could do:
WHERE invoice_id IN ({list}) ORDER BY FIND_IN_SET(invoice_id, '{list}')
You want the FIELD order by parameter.
SELECT * FROM invoice WHERE invoice_id IN (13, 15, 9, 27) ORDER BY FIELD (invoice_id, 13, 15, 9, 27)