I'm attempting to select all the rows based on whether or not it falls within the limits of two numbers. For example, I want all the rows who's limits include the number 4. So if a row that had a start value of 2 and end value of 7 would be returned. Here's what I have so far:
SELECT *
FROM egw_scripture_reference
WHERE book = 'Revelation'
AND end_verse <= 4
AND start_verse >= 4
If a row that had a start value of 2 and end value of 7 should be returned then your filter condition should be as shown here.
SELECT * FROM egw_scripture_reference
WHERE book = 'Revelation' AND end_verse >= 4 AND start_verse <= 4
I think your query is wrong, as you said: if a row that had a start value of 2 and end value of 7 would be returned you will need this query
SELECT *
FROM egw_scripture_reference
WHERE book = 'Revelation'
AND end_verse >= 4
AND start_verse <= 4
Related
I have the following table as an example:
numbers type
--------------
1 1
5 2
6 1
8 2
9 3
14 2
3 1
From this table I would like to select the closest number that is less or equal to 5 AND of type 1 and if there is no such row matching, then (and only then) I would like to return the first closest number larger than 5 of type 2
I can solve this by running two queries:
SELECT number FROM numbers WHERE number <= 5 AND type = 1 ORDER BY number LIMIT 1
and if above query returns 0 results, I simply run the second query:
SELECT number FROM numbers WHERE number > 5 AND type = 2 ORDER BY number LIMIT 1
But is it possible, to achieve the same result by only using one query?
I was thinking something like
SELECT number FROM numbers WHERE (number <= 5 AND type = 1) OR (number > 5 AND type = 2) ORDER BY number LIMIT 1
But that would only work, if mysql first checks the first conditional in the parentheses against all rows and if it finds a match, it returns it, and if not, then it checks all rows against the second parenthesed conditional. It will not work, if it checks each row against both parentheses and only then moves to the next row, which is how I suspect it works.
This query will do what you want. It selects all numbers that match your two query constraints, and orders the results first by type (so that if there is a result for type 1 it will appear first) and then by either -number or number dependent on type (so that numbers <= 5 sort in descending order but numbers > 5 sort in ascending order):
SELECT number
FROM numbers
WHERE ( number <= 5 AND type = 1 )
OR ( number > 5 AND type = 2 )
ORDER BY type, CASE WHEN type = 1 THEN -number ELSE number END
LIMIT 1
Output:
3
Demo on dbfiddle
Combine the two, and you always prefer type 1 over type 2, hence the ORDER BY and LIMIT. The ABS means whichever is first by type, is the closes to the number 5.
SELECT number, type
FROM numbers
WHERE (number <=5 AND type=1) OR
(number > 5 AND type=2)
ORDER BY type ASC, ABS(number-5) ASC
LIMIT 1
i have this table called "tblpercentage"
PercentID Dividend Rebates Interest
1 40 10 2
2 60 20 5
this is my query
SELECT MAX(PercentID)As PercentID,Interest FROM tblpercentage
but the problem is. it can only select PercentID = 2 and interest = 2 instead if 5. but i want is select all row with the max id. can anyone help me.
for get the row related to the max id you should use a subquery
select PercentID,Interest from tblpercentage
where PercentID = (SELECT MAX(PercentID) from tblpercentage)
When using the <= in a MySql statement, is it possible to make MySql select the maximum value in a table without supplying a value to <= in the sql statement?
Eg:
id
----
1
2
3
4
5
6
Eg:
// start from the last record when no value is supplied
select id from table where id <= * ORDER BY id DESC LIMIT 5
Result
6
5
4
3
2
// start from the 5th record when a value is supplied
select id from table where id <= 5 ORDER BY id DESC LIMIT 5
Result
5
4
3
2
1
My problem is, this statement is in a php function, so I cannot change it dynamically. Is it somehow possible to do what I'm trying, or perhaps another way to get around this?
You can use NULL instead of using *
SET #var_search_value = NULL -- OR 5
SELECT id
FROM table
WHERE id <= #var_search_value OR #var_search_value IS NULL
ORDER BY id DESC LIMIT 5
If you want to get every record which is less than or equal to the maximum value in a particular column, then logically you want to get every record:
select id from table ORDER BY id DESC LIMIT 5
No WHERE clause is required.
I have two columns Page_from and Page_to. When the records are sorted, they appear as...
Page_from Page_to
1 4
5 7
9 11
Here page number 8 is missing.
I want to find the missing page number.So I must be able to compare the value of Page_to in previous row with Page_from in current row.
You can find the beginning of a missing sequence by finding the previous record, and comparing the previous page_to and the current page_from. If there is a gap, you can get both the first and last page in the gap.
select tprev.page_to + 1 as missing_page_from, t.page_from - 1 as missing_page_to
from (select t.*,
(select tprev.page_to
from t tprev
where tprev.page_from < t.page_from
limit 1
) as prev_to
from t
) t
where prev_to is not null and
prev_to <> t.page_from - 1;
I want to count from the row with the least value to the row with a specific value.
For example,
Name / Point
--------------------
Pikachu / 7
Voltorb / 1
Abra / 4
Sunflora / 3
Squirtle / 8
Snorlax / 12
I want to count to the 7, so I get the returned result of '4' (counting the rows with values 1, 3, 4, 7)
I know I should use count() or mysql_num_rows() but I can't think of the specifics.
Thanks.
I think you want this :
select count(*) from mytable where Point<=7;
Count(*) counts all rows in a set.
If you're working with MySQL, then you could ORDER BY Point:
SELECT count(*) FROM table WHERE Point < 7 ORDER BY Point ASC
If you want to know all about ORDER BY, check out the w3schools page: http://www.w3schools.com/sql/sql_orderby.asp
Just in case you want to only count the rows based on the Point values:
SELECT count(*) FROM table WHERE Point < 7 GROUP BY Point
This may help you to get rows falling between range of values :
select count(*) from table where Point >= least_value and Point<= max_value