I have a query like this:
SELECT [...]
FROM [...]
WHERE
FirstInt BETWEEN 100 AND 200 AND
SecondInt BETWEEN 100 AND 200
Those variables have to be in the same range. Is there any way to write sth. like "(a || b) between 100 and 200"?
I am not sure if this is what you were looking for:
SELECT [...]
FROM [...]
WHERE least(FirstInt , SecondInt, ... ) >= 100
AND greatest(FirstInt , SecondInt, ... ) <= 200
With this query you will find the smallest value of all of your columns(with function least) and compare it to the smallest value of your range. You will do the same with the largest value of all of your columns (with function greatest) and compare it to the largest value of your range.
Here is a small demo:
DEMO
With this query you can replace your multiple between statements or in other words you can use it when you want to copare multiple columns to the same range.
I may not fully understand the question but i think you can use this.
SELECT [...]
FROM [...]
WHERE
FirstInt >= 100 AND FirstInt<=200 OR
SecondInt >= 100 AND SecondInt <= 200
Related
Does mysql support !<= or !>= operator!!!
I am trying to fetch the data from the Person table where the age of a person is not greater than 30 (the age field may have null value).
You may phrase not greater than 30 as being aged 30 or younger:
SELECT *
FROM Person
WHERE age <= 30;
By default, those records with a null age would not be included in the above inequality.
If you really wanted to use NOT, then we could try:
SELECT *
FROM Person
WHERE NOT age > 30;
But typically you will just see the appropriate inequality being used, without an explicit NOT.
Not greater than can be written as <=
!<= is not an operator
Why not doing something like this :
SELECT * FROM table WHERE id <= 100
That mean the query will selecting the ID which not GREATER THAN 100. Or if you want to search something with specific value, you can try this :
SELECT * FROM table WHERE id >= 50 AND id <= 100
That mean the query will search data from table which ID is MORE THAN 50 AND NOT GREATER THAN 100.
Hope this will help.
An example
SELECT * FROM my_table WHERE date>=01012018 OR date<=31012018
Using "AND"
SELECT * FROM my_table WHERE date>=01012018 AND date<=31012018
How will my records be affected?
It's a big impact.
When you use "condition AND condition" the query will return only the matching results (in your case, only results where the date is >= than 01012018 AND date is <= 31012018, like 10012018, 20012018, 15012018, etc).
If you use "condition OR condition", it will return the results where the date is >= 01012018 (like 02012018, 20012018, 15022018, 30122018, etc) OR the date is <= 31012018 (like 01012000, 15022015, 17052017, etc) -> basically, all the results.
I need an SQL query , with multiple AND's. Let me explain with an example,
For example I want to search in a database for a property , who's price is greater than 1000 and less than 2000 (price is between 1000-2000), and its area is greater than 1000 sqft. and less than 2000 sq ft. (area is between 1000-2000).
So i was guessing that the query could be,
SELECT *
FROM table_name
WHERE (price>1000 AND price<2000) AND (area>1000 AND area<2000)
this is something i need ! Thank you
Your original query looks fine to me, but you can also use BETWEEN if you like, try this:
SELECT * FROM table_name WHERE (price BETWEEN 1001 AND 2000) AND (area BETWEEN 1001 AND 2000);
expr BETWEEN min AND max
If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <= expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 12.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments
Use between instead of and
SELECT * FROM table_name WHERE (price between 1001 AND 1999) AND (area between 1001 AND 1999)
Use this, i think it will solve your problem. It works for me:
SELECT * FROM table_name WHERE price and area BETWEEN 1001 and 1999.
If we have same values of the parameters, then we can add the condition with and all the parameters.
I am trying to get the MIN/MAX/AVG from my database, but there may be some spurious results from time to time. Basically, I only want MYSQL to give me the minimum etc of a difference calculation.
The criteria I need would be +65.00 each way.
Here is some sample data
Actual
1854
1843
1865
1822
1833
1859
1400
Here is my query -
Select MIN(ACTUAL - 1800), MAX(ACTUAL - 1800), AVG(ACTUAL - 1800) FROM ACTUAL_TABLE WHERE DATE = '2015-08-09'
This query result would be -
MIN MAX AVG
-400 65 -17.71428571
So clearly, the query is picking up the 1400 in the actual table, and the difference is -400, I need it to ignore this result as its past the > -65.00 criteria I need.
If the criteria was in place the result would look like this -
MIN MAX AVG
22 65 -17.71428571
I have tried putting a CASE in the SQL and had no joy. Can anyone shed some light on this issue?
Thankyou.
Select only the values you want
Select MIN(ACTUAL - 1800), MAX(ACTUAL - 1800), AVG(ACTUAL - 1800)
FROM ACTUAL_TABLE
WHERE DATE = '2015-08-09' AND ACTUAL >= (1800-65) AND ACTUAL <= (1800+65);
I came across a mysql query that looks like this:
SELECT
SUM(some_amount*(some_field!=90)*(some_date < '2011-04-22'))
, SUM(some_amount*(some_field =90)*(some_date < '2011-04-22')*(another_field IS NULL))
FROM
some_table
What does the * mean in the select statement in this case?
Looks like CAST() is not necessary for boolean-to-integer conversions. Multiplication is used to convert the sum to 0 for unwanted rows (using the fact that boolean true can be cast to 1 and false to 0):
some_amount*(some_field!=90)*(some_date < '2011-04-22')
if some_field == 90 or some_date >= '2011-04-22', the corresponding term will evaluate to 0, thereby converting the entire expression to 0.
It is a multiplication operation.
example 2*3=6
It's a standard multiplication operator,
select 2 * 2
= 4
:)