Learn the value in the table not by ID number - mysql

Here is a table:
5000
4900
4800
4700
3800
3600
2000
1900
1600
1000
The table does not have identification numbers.
The table is sorted by the decay, the greatest value from above.Question. How to make a mysql query to find a value of 5 on the list item? The column ID, no ...
Sorry for my bad english.
Thanks!

I am going to interpret this as finding the fifth value in the list. You can do this using limit:
select t.*
from t
order by col desc
limit 4, 1;

Do you mean you want to find a row which had value 5?
SELECT *
FROM TABLE_NAME
WHERE ID = 5
or if you mean you want the fifth element in the list,
SELECT *
FROM TABLE_NAME
LIMIT 5,1

Related

How can I finish my MySQL fetch after searching a specific amount of rows even if the row is not found?

I have a MySQL Database with over 100k rows, so I need to make a search to fetch only the last 1000 rows , so if it is not found in the last 1000 rows the fetch ends (even if it is not found)
Example: if my table is like that
id name
1 AL
2 BL
...
1000 P12
1001 P15
And I do a fetch like this: SELECT * FROM myTable WHERE name = 'AL' ONLY LAST 1000 ROWS ORDER BY id DESC (Since I don't know what to use I invented the ONLY LAST 1000 ROWS)
This should return empty because I wanted my query to get the information only if it was on the last 1000 rows, not on the 1001th as specified.
Using LIMIT field doesn't work as it would LIMIT the FOUND ROWS not when they are not found.
Is there a way to implement this in MySQL ?
Thank you!
As touched on in the comments, you can use OFFSET to get the id of the 1000th last record, then SELECT records with an id larger than that record's id.
Something like this:
SELECT name
FROM myTable
WHERE id > (SELECT id FROM myTable ORDER BY id DESC LIMIT 1 OFFSET 1000)
AND name = 'AL'

mysql select 6th thru 10th (natural order) records

I have a simple table:
ID | Name
0183 namez
2543 etc
2654 etc
4364 namez
3246 namey
3745 namew
3464 namem
7524 etc
2459
2457
0845
9325
I need to be able to select the 6th thru 10th rows or the 4th thru 25th or whatever, so that I can select only the rows that I need without using any kind of Id column, also it's alway Xth "thru" Yth, because I'm not hardcoding an column names here, I can't use order by but have to use natural order. Is this even possible? Thanks for any help.
you need to pass a LIMIT clause to your SELECT query. In MySQL this would be:
SELECT * FROM simpletable LIMIT 5, 5;
NOTE:
the first number is the offset, it needs to be the first row minus one, (i.e. 6 - 1).
the second is the number of rows returned, this needs to be last row - offset (i.e. 10 - 5).
SEE: http://dev.mysql.com/doc/refman/5.0/en/select.html
SELECT *
FROM tables
ORDER BY ID
LIMIT 5, 5
You can do the following:
SELECT * FROM tables ORDER BY ID LIMIT 4, 10

MySQL Query Nearest Lowest Value & Actual Value Issue

I have table
'test' with fileds & values
id number
1 13
2 17
3 20
4 30
5 40
If i provide 14,15,16 then it should give me 13.
If i provide 22,24,24 then it should give me 20.
If i provide 13 then it should give me 13.
If i provide 20 then it should give me 20.
I'm looking for mysql query for this which provide nearest lowest value & actual value.
Assuming you send a table with a list of values
SELECT
*
FROM
MyTable
WHERE
number <= (SELECT MIN(requireNuber) FROM InputTable)
ORDER BY
number DESC
LIMIT 1
Although, I'm sure you could only send the lowest value to MySQL from the client (why can't you) which would make the code look like
SELECT
*
FROM
MyTable
WHERE
number <= #MyParameter
ORDER BY
number DESC
LIMIT 1

MySql order by specific ID values

Is it possible to sort in MySQL by "order by" using a predefined set of column values (ID) like order by (ID=1,5,4,3) so I would get records 1, 5, 4, 3 in that order out?
UPDATE: Why I need this...
I want my records to change sort randomly every 5 minutes. I have a cron task to update the table to put different, random sort order in it.
There is just one problem! PAGINATION.
I will have visitors who come to my page, and I will give them the first 20 results. They will wait 6 minutes, go to page 2 and have the wrong results as the sort order has already changed.
So I thought that if I put all the IDs into a session on page 2, we get the correct records even if the sorting had already changed.
Is there any other better way to do this?
You can use ORDER BY and FIELD function.
See http://lists.mysql.com/mysql/209784
SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)
It uses Field() function, Which "Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found" according to the documentation. So actually you sort the result set by the return value of this function which is the index of the field value in the given set.
You should be able to use CASE for this:
ORDER BY CASE id
WHEN 1 THEN 1
WHEN 5 THEN 2
WHEN 4 THEN 3
WHEN 3 THEN 4
ELSE 5
END
On the official documentation for mysql about ORDER BY, someone has posted that you can use FIELD for this matter, like this:
SELECT * FROM table ORDER BY FIELD(id,1,5,4,3)
This is untested code that in theory should work.
SELECT * FROM table ORDER BY id='8' DESC, id='5' DESC, id='4' DESC, id='3' DESC
If I had 10 registries for example, this way the ID 1, 5, 4 and 3 will appears first, the others registries will appears next.
Normal exibition
1
2
3
4
5
6
7
8
9
10
With this way
8
5
4
3
1
2
6
7
9
10
There's another way to solve this. Add a separate table, something like this:
CREATE TABLE `new_order` (
`my_order` BIGINT(20) UNSIGNED NOT NULL,
`my_number` BIGINT(20) NOT NULL,
PRIMARY KEY (`my_order`),
UNIQUE KEY `my_number` (`my_number`)
) ENGINE=INNODB;
This table will now be used to define your own order mechanism.
Add your values in there:
my_order | my_number
---------+----------
1 | 1
2 | 5
3 | 4
4 | 3
...and then modify your SQL statement while joining this new table.
SELECT *
FROM your_table AS T1
INNER JOIN new_order AS T2 on T1.id = T2.my_number
WHERE ....whatever...
ORDER BY T2.my_order;
This solution is slightly more complex than other solutions, but using this you don't have to change your SELECT-statement whenever your order criteriums change - just change the data in the order table.
If you need to order a single id first in the result, use the id.
select id,name
from products
order by case when id=5 then -1 else id end
If you need to start with a sequence of multiple ids, specify a collection, similar to what you would use with an IN statement.
select id,name
from products
order by case when id in (30,20,10) then -1 else id end,id
If you want to order a single id last in the result, use the order by the case. (Eg: you want "other" option in last and all city list show in alphabetical order.)
select id,city
from city
order by case
when id = 2 then city else -1
end, city ASC
If i had 5 city for example, i want to show the city in alphabetical order with "other" option display last in the dropdown then we can use this query.
see example other are showing in my table at second id(id:2) so i am using "when id = 2" in above query.
record in DB table:
Bangalore - id:1
Other - id:2
Mumbai - id:3
Pune - id:4
Ambala - id:5
my output:
Ambala
Bangalore
Mumbai
Pune
Other
SELECT * FROM TABLE ORDER BY (columnname,1,2) ASC OR DESC

mysql query - arranging rows by field and counting id x place

id field
1 100
2 80
3 200
4 230
Is it possible to find out the rank of id 2 for example if ordered by field DESC?
In this case the ranks would be:
1. #4
2. #3
3. #1
4. #2 (our winner)
If I understand your question correctly, this is what you would need. Note that since MySQL doesn't have a row numbering function, you need to use a variable in its place
SET #rank=0;
SELECT * FROM (
SELECT #rank:=#rank+1 AS rank, id, field FROM table ORDER BY field DESC
) vw
WHERE vw.id = 2;
rank is automatically in that case is created when used ORDER BY DESC. id's will keep selected in proper ranking order.
SELECT id,field FROM table_name ORDER BY id DESC