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
Related
I have Database table payment such as below
level_count | amount
__________________________
650 | 12
1000 | 35
1700 | 50
__________________________
Now Wanted to check if I supplied input as 650 which is level_count column value. Then I should get amount as 12. Then If I supplied input as 999 I should still get 12. Means It should compare its successive rows and compare. Suppose If I enter 1200 then I should get 35 and If I enter 1700 or above I should get 50.
I have tried flowing but didn't got any success.
Where I am going wrong.
SELECT * FROM payment T1
INNER JOIN payment T2 on T1.level_count>=T2.level_count AND T1.level_count<T2.level_count
WHERE T1.level_count = '650'
When I execute above query I get no results.
You may try using a LIMIT query here:
SELECT *
FROM payment
WHERE level_count <= 999 -- or 650, or another input value
ORDER BY level_count DESC
LIMIT 1;
The logic of the above query works in two parts. First, the WHERE clause removes all records for which the level_count is greater than the input value. But this still leaves us potentially with more than one record (which include the record we actually want). The LIMIT trick then keeps the single remaining record with the highest level_count.
I'm trying to find an elegant way to write a query that only returns enough rows for a certain column to add up to at least n.
For example, let's say n is 50, and the table rows look like this:
id count
1 12
2 13
3 5
4 18
5 14
6 21
7 13
Then the query should return:
id count
1 12
2 13
3 5
4 18
5 14
Because the counts column adds up to n > 50. (62, to be exact)
It must return the results consecutively starting with the smallest id.
I've looked a bit into accumulators, like in this one: MySQL select "accumulated" column
But AFAIK, there is no way to have the LIMIT clause in an SQL query limit on an SUM instead of a row count.
I wish I could say something like this, but alas, this is not valid SQL:
SELECT *
FROM elements
LIMIT sum(count) > 50
Also, please keep in my the goal here is to insert the result of this query into another table atomically in an automated, performance efficient fashion, so please no suggestions to use a spreadsheet or anything that's not SQL compatible.
Thanks
There are many ways to do this. One is by using Correlated Subquery
SELECT id,
count
FROM (SELECT *,
(SELECT Isnull(Sum(count), 0)
FROM yourtable b
WHERE b.id < a.id) AS Run_tot
FROM yourtable a) ou
WHERE Run_tot < 50
I'm looking for a mysql select that will allow me to select (LIMIT 8) records after some changing number of first few matches;
select id
from customers
where name LIKE "John%"
Limit 8
So if i have a table with 1000 of johns with various last names
I want to be able to select records 500-508
You can send the offset to the limit statement, like this:
SELECT id
FROM customers
WHERE name LIKE "John%"
LIMIT 8 OFFSET 500
Notice the OFFSET 500 on the limit. That sets the 'start point' past the first 500 entries (at entry #501).
Therefor, entries #501, #502, #503, #504, #505, #506, #507 and #508 will be selected.
This can also be written:
LIMIT 500, 8
Personally, I don't like that as much and don't understand the order.
Pedantic point: 500-508 is 9 entries, so I had to adjust.
As a solution please try executing the following sql query
select id from customers where name LIKE "John%" Limit 500,8
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
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