i have table with id, date_from, date_to fileds.
I need to find all the data where date_to +1 month is equal to condition
for example>
if data in date_to field is 2015-06-01 and WHERE condition is 2015-07-01 i should get the result
SELECT *, (date_to + INTERVAL 1 MONTH) as 'next' FROM table_name WHERE 'next'='2015-07-01'
this is my query, that obviously doesn't work ;)
Tnx in adavance
First thing you can not use alias in the where condition and also the condition is wrong. It will be
SELECT * FROM table_name
WHERE
date_add(date_to,interval 1 month)='2015-07-01'
Now note that for large data-set its not a good idea to use function on the date filed since even if the field is index it will fail to use index. So better to use the function on the data to be filtered rather than on the field. So the above query could be written as
SELECT * FROM table_name
WHERE
date_to= date_sub('2015-07-01',interval 1 month)
Related
I have a from_date and to_date columns in my LeaveApplication table. So I'm trying to check if the specified date is in range of the two columns. For example:
from_date : 2021-08-26
to_date : 2021-08-29
date : 2021-08-27
I was trying to use the BETWEEN operator but the syntax is to select the date range from the same column name.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
In my case, I want to select the date range from different columns something like the query example below. Is there a way to achieve this?
SELECT * FROM LeaveApplication
WHERE "2021-08-27" BETWEEN (from_date = "2021-08-26") AND (to_date = "2021-08-29")
Thank you!
Using greater, equal, and less operator didn't work for me but actually it can be done with BETWEEN operator this way
SELECT * FROM LeaveApplication WHERE "2021-08-27" BETWEEN from_date AND to_date
Found this example here
p/s: my attempt in my example is quite close to it :)
select * from LeaveApplication where from_date >="2021-08-26" and to_date <="2021-08-26"
If the result of rows is empty, the date exists, else not!
Is it possible to do something like this?
select * from table where Date BETWEEN '2019-05-29' AND '2019-05-29'
Yes it is possible. If you have time part you could use DATE function to skip it:
SELECT * FROM table WHERE DATE(Date) BETWEEN '2019-05-29' AND '2019-05-29'
-- it may degrade performance, condition is not SARGable
Yes, but the better approach is:
select t.*
from table t
where t.Date >= date('2019-05-29') AND
t.Date < date('2019-05-29') + interval 1 day
Why is this better? It doesn't have a function on the column name, so it can make use of an index on the date column.
Yes you can, if you want to run it in a test window without manually changing the date within the code you can set it as a variable. Use trunc to get rid of time i.e there will be no 29-05-2019 23:59:00. If you want the same date within a time period remove the trunc and then you can set hours-minutes-seconds which makes your query more precise
SELECT t.*
FROM table t
WHERE t.date BETWEEN trunc(to_date(:datefrom, 'dd.mm.yyyy hh24:mi:ss')) AND
trunc(to_date(:dateto, 'dd.mm.yyyy hh24:mi:ss'))
I have table in database which has fields -(EId,PId,Date,Time).
I want to select all the fields from this table where month < 5.To be more elaborate I want entries which were entered into the table before month 5.
Please help me write this sql query. Thanks :)
Use MONTH() and also remember to restrict the year in the WHERE clause:
SELECT *
FROM yourTable
WHERE MONTH(Date) < 5 AND YEAR IN (...)
I have a table which contains date (Field Type: Date and Date Format: %Y-%m-%d) as a field. I need to select all the rows from the table for all the years whose date is not between Dec 3rd and Dec 24th.
The table contains month and day as a separate fields.
The result can be obtained by using the following query:
select * from mytable where date not in (select date from mytable where month=12 and day between 3 and 24);
But i m trying to get the result in a single query like the below one but it gave empty rows:
select * from mytable where date not between '%Y-12-03' and '%Y-12-24';
Can it be done in a single query like the above one?
SELECT *
FROM mytable
WHERE MONTH(`date`) <> 12
OR DAY(`date`) NOT BETWEEN 3 AND 24
;
This will give you every row that meets the requirements. I'm sure someone has a faster way of doing this, since this will ignore all indexes and will likely be slow on a large dataset, but it does work and return the data you require, so if no-one can suggest an improvement this will answer your question.
I want to perform a different SELECT based on the column data. For example I have a table http://sqlfiddle.com/#!2/093a2 where I want compare start_date and end_date only if use_schedule = 1. Otherwise select all data. (A different select) Basically I only want to compare the start and end date if only use_schedule is 1 and if use_schedule is 0 then select rest of the data.
An example may be something like
select id, name from table
where use_schedule = 0
else
select id, name, start_date from table
where use_schedule = 0 and current_date >= start_date.
Basically I have the data where schedule is enabled only then look into start and end date. Because if schedule is not enabled there is no point of looking into the dates. Just select the data. With schedule enabled, I want to be more selective in selecting the scheduled data.
I am trying to figure out if MySQL CASE or IF statements would work but not able to do so. How can I run this select?
Thanks.
You can use UNION to mix and match the results of 2 different SQL queries into one result set:
select id, name, null from table
where use_schedule = 0
union
select id, name, start_date from table
where use_schedule = 1 and current_date >= start_date
Note that both queries have to have compatible output fields (same number and type for this to work). The use of UNION automatically merges only distinct records - if you want to keep double results use UNION ALL instead.
In this specific case a more extensive WHERE-clause would also work obviously:
where use_schedule = 0 or (use_schedule = 1 and current_date >= start_date)
But given the question I'm assuming your real case is a bit more complex.
Documentation over at MySQL site.
Use CASE, in this case..:
SELECT id, name,
(CASE
WHEN start_date >= DATE(NOW()) AND use_schedule = 1
THEN start_date
ELSE NULL
END) AS cols FROM campaigns
This way it selects only the schedule 0 OR the 1 with a date bigger or equals to now;
I used DATE(NOW()) so that it removes the time which you are not interested in.