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.
Related
I'll try to explain by example what I want to achieve in MySQL.
I have a table looking like this:
pricelist_id valid_from
1 1610665200000 //15 Jan 2021
2 1610751600000 //16 Jan 2021
3 1610838000000 //17 Jan 2021
Values in column, valid_from, are essentially String values which were generated at some point in time by following Java code:
String.valueOf(System.currentTimeMillis())
My assignment is to pass any String value (in the format of a number, in this case long), generated in the same way as explained above and I need to extract single pricelist_id which belongs to the first lesser or equal value of valid_from in the table, in comparison with passed value of valid_from. It is hard to explain by words (and my English is just terrible), so I'll demonstrate.
If I pass a value which will represent Jan 18th, 1610924400000, I would need to get single pricelist_id value, in this case 3.
If I pass a value which will represent Jan 16th 17:15:00, 1610813700000, I would need to get single pricelist_id value, in this case 2.
If I pass a value which will represent Jan 25th, 1611529200000, I would need to get single pricelist_id value, in this case 3.
What I have so far, is this:
select max(p.pricelist_id)
from pricelist p
where (p.valid_from + 0) <= some_passed_value
order by p.valid_from, p.pricelist_id desc
If you can rely on pricelist_id being in the exact same sort order as valid_from, then your solution using max() works.
Otherwise you can use LIMIT:
select p.pricelist_id
from pricelist p
where (p.valid_from + 0) <= some_passed_value
order by p.valid_from, p.pricelist_id desc
limit 1
Convert the string to number for ordering and for comparing
Also use limit to get the higher value
select p.pricelist_id
from pricelist p
where CONVERT(p.valid_from, SIGNED) <= some_passed_value
order by CONVERT(p.valid_from, SIGNED), p.pricelist_id desc
LIMIT 0, 1
please, I want to select from mysql tables where the absolute difference between two columns is the smallest value between the absolute difference values.
I tried this syntax but it was not right
SELECT strike FROM options_20161230 ORDER BY ask - bid ASC LIMIT 1
I wonder if I can create a new column in the table as the difference between two columns, is that possible?
also I want to select where one column has a value between two numbers, I tried this
SELECT strike FROM options_20161230 WHERE 7 < Expiration - Datadate < 37 AND type ='put' AND UnderlyingSymbol = 'SPY'
it works when limited Expiration - Datadate by one value < 37. however It was not working with two values <,> ?
any idea please!
Many Thanks
Your first query is close. You just want abs():
SELECT strike
FROM options_20161230
ORDER BY abs(ask - bid) ASC
LIMIT 1;
Your third query should use between (assuming the difference is an integer) or two inequalities:
SELECT strike
FROM options_20161230
WHERE Expiration - Datadate BETWEEN 8 AND 36 AND
type ='put' AND
UnderlyingSymbol = 'SPY';
I am trying to do a select from CTE based on a condition.
There is a variable I've declared for today's period (#PRD). It holds the value of what period we are currently in.
Now I would like to do a selection from a table that will restrict what information is returned based on whether we are in the first half of the year or not.
For instance, we are in period 2 so I want everything returned from my CTE which falls between PRD 1 and 5. IF we were in say period 6 (after 5), then yes I'd want everything returned from the table.
This is the pseudocode of what I'm trying to accomplish:
SELECT
CASE
WHEN #PRD <= 5
THEN (SELECT * FROM DISPLAY WHERE PERIOD IN (1,2,3,4,5))
ELSE (SELECT * FROM DISPLAY)
END
I'm getting an error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Please any thoughts on how I can do this?
Thanks x
EDITED/UPDATED:
More of the code involves a CTE and is really long. Bottom line is lets say I have this CTE
;WITH DISPLAY as (
select * from lots_of_things
)
SELECT * FROM DISPLAY
Having done a regular select on this CTE, it returns data that looks like this:
PERIOD (INT) DEPARTMENT GROUP BUDGET
1 ENERGY HE 500
2 ENERGY HE 780
3 ENERGY HE 1500
4 ENERGY HE 4500
5 ENERGY HE 400
6 ENERGY HE 3500
7 ENERGY HE 940
8 ENERGY HE 1200
I want it to show me just the top 5 rows if we the current period is 1,2,3,4,5. But to display ALL table rows if we are in any other period like 6,7,8,9 and onwards. The current period is held in the variable #PRD which is derived from doing a comparison of today's date with ranges held in a table. The value is accurate and also type INT
Hope this helps
SQL FIDDLE
This will work:
SELECT * FROM DISPLAY WHERE (#PRD > 5 OR PERIOD IN (1, 2, 3, 4, 5))
If this code confuses you, what's happening is that we check if #PRD > 5 and if that returns true, our expression is always true so we return all the rows.
If the variable is less or equal to 5 (like you checked in your example), the first check is false and then we check if the period is the list.
This might be a solution:
IF #PRD <= 5
SELECT * FROM DISPLAY WHERE PERIOD IN (1,2,3,4,5)
ELSE
SELECT * FROM DISPLAY
UPD
In this case you should use variable instead of CTE, if it's possible.
DECLARE #PRD INT;
SELECT #PRD = PERIOD FROM SOME_TABLE WHERE ...
Here is my table info,
__test
id points date
1 -50 30.09.2013
2 100 2.10.2013
3 100 3.10.2013
4 200 4.10.2013
From this i need to select records,if any of the value contains minus points based on the date
For ex:
select *
from #__test
where date between 30.09.2013 to 3.10.2013
Mentioned query is common for getting in between records fro two dates.But i need the records between two dates,if any of the value contains minus points.
How can i do this ? Kindly help me.
I think this is what you are after:
SELECT *
FROM #__test t
WHERE t.Date BETWEEN '20130930' AND '20131003'
AND EXISTS
( SELECT 1
FROM #__test t2
WHERE t2.Date BETWEEN '20130930' AND '20131003'
AND t2.points < 0
);
It simply checks for the existance of a negative value in the date range and then returns all records for that date range if one is found.
N.B This article is probably worth a read regarding using BETWEEN for date ranges, and there is further reading on date range queries in another article written by Aaron Bertrand
So.. to return the age based on a date type it's really easy :
SELECT YEAR(CURDATE()) - YEAR(DateofBirth) and we get a result set with the age of the person
But what if I need to search the people with an age range of 60-70 in the table based on their date of birthdays in this format d/m/y ( Ex: 2005-04-11). I considered it was kind of hard because it's a different thing searching for an certain age if u have only dateofbirths instead of searching through a column where u already have their ages being counted as normal years ( 50, 60 etc )
Another problem that's not related to the one mentioned above:
Let's say I have a table with Medics that has their names, surnames and their speciality.
We got 17 specialities : reumatologie, o.r.l, chirurgie etc. and 108 doctors
How do I get the list of the medical specialities and the doctors that are in that range based on this format:
Ex: reumatologie - 4, o.r.l - 5, chirurgie - 10 etc
4 and 5 from the examples are the number of medics. I've tried concatenating and all sort of queries none of them worked properly though. Maybe will work with a case? if yes how?
For question 1, you can just do (not very good regarding performance though):
SELECT YEAR(CURDATE()) - YEAR(DateofBirth) AS age
FROM tableX
WHERE (YEAR(CURDATE()) - YEAR(DateofBirth)) BETWEEN 60 AND 70 ;
If you want performance, you can add an index on DateofBirth and use this version:
SELECT YEAR(CURDATE()) - YEAR(DateofBirth) AS age
FROM tableX
WHERE DateofBirth >= MAKEDATE(YEAR(CURDATE())-70, 1)
AND DateofBirth < MAKEDATE(YEAR(CURDATE())-60+1, 1) ;