So basically I have a table -
ID | from | To
-----------------
1 | 25.05.2012|30.05.2012
-----------------
2 | 15.05.2012|20.05.2012
-----------------
3 | 25.06.2012|30.06.2012
and I have a query
SELECT date.*
FROM table AS date
WHERE (date.from >= '25.05.2012' OR date.to >= '25.06.2012' ) AND (date.to <= '30.05.2012' OR date.from <= '25.05.2012' )
GROUP by date.id
but it's not working, what could be the problem?
date is a reserved keyword in MySQL, try calling the table something else!
You are performing string comparisons, not date comparisons, eg: 26.05 is greater than 25.06 since the comparison only reaches as far as the second character of the string and determines that 6 is greater than 5.
In order to perform date comparisons you either need to store the value as a date (preferable) or in an appropriate string form eg yyyyMMdd.
SELECT * FROM table WHERE (from >= '25.05.2012' AND to <= '25.05.2012') GROUP by id;
"date" is a reserved string in MySQL. I'd recommend renaming all tables and columns with that name to a custom one. This might already solve your problem.
Point 1 : table, date, from are reserved keywords. Those should not be used for naming tables. If your table name is table, use backticks
Point 2 : When you have one table why you are creating alias for that?
Point 3 : While comparing date should be in format of yyyy-mm-dd. I believe from and to columns are timestamp.
Point 4 : No need of GROUP BY statement at the moment as you are not using any aggregate function such as SUM, COUNT, etc
Your query should be
SELECT *
FROM `table`
WHERE
(`from`>= '2012-05-25' OR `to`>= '2012-06-25' )
AND
(`to` <= '2012-05-30' OR `from` <= '2012-05-25');
If you want to pass date as 25.05.2012 , use STR_TO_DATE function to convert string to date.
Related
I've column that contains data like so,
07/2002
05/2005
04/2000
month/year
can I filter out data using query, i.e
SELECT * FROM `table` WHERE `fr` < '07/2002'
it should return 04/2000
is't possible using MySQL or I've to use other language to select and filter out data like PHP?
plan
use str_to_date
to convert the start of month values to dates and compare
query
select *
from `table`
where str_to_date(concat('01/', fr), '%d/%m/%Y')
<
str_to_date('01/07/2002', '%d/%m/%Y')
;
output
+---------+
| fr |
+---------+
| 04/2000 |
+---------+
sqlfiddle
note
while the above is a solution to the question as asked, it is just dealing with the symptoms not the underlying issue.
the real issue is the storage type being used to store date information
consider using actual dates to store this information. this causes the following symptoms :
symptoms
complexity : we have to perform further manipulations to transform into the date type we can use
performance ( see above )
maintenance ( see this question )
we can instead fix this issue where it is caused by changing the storage type to correctly reflect the semantic contents ( its date information and should be able to be compared in this way simply )
fix
alter table `table` add column fr_datetype date not null;
update `table`
set fr_datetype = str_to_date(concat('01/', fr), '%d/%m/%Y')
;
-- test conversion to date type
select
'error converting' as test_conversion
from `table`
where concat(lpad(extract(month from fr_datetype), 2, '0'), '/', extract(year from fr_datetype)) <> fr
;
-- only finalise this based on successful completion of above select ( no rows returned )
alter table `table` drop column fr;
alter table `table` change column fr_datetype fr date not null;
simplified solution
select *
from `table`
where fr < '2002-07-01'
;
Use MySQL built in date format, so you can perform queries like this:
WHERE DATE_FORMAT(date_field, '%m') < 7
AND DATE_FORMAT(date_field, '%y') < 2002
Or a simpler solution is to use timestamps (that stores seconds), and you can do things like:
WHERE timestamp_field < 1027407492
Or if you'd like to use dates as you're using it now (not recommended), store them in two columns. One for month and other one for year, then you can query like this:
WHERE month_field < 7
AND WHERE year_field < 2002
I recommend timestamps.
For example I have two columns in my DB:
Date1 and Date2. I put into my input fields (field1 = 12/11/14 and field2 = 22/11/14). So I want to take all rows into my table who have lower date than 12/11/14 and higher than 22/11/14. How should my query look?
I will make my question more clear.
Here is my DB:
So I put into rent_date a client who will rent a car from 11/12/14 and will return it at 11/22/14. There are 5 clients and only one will rent a car between exactly 11/12/14 and 11/22/14 so I want to select the other 4 clients who doesn't rent car between this two dates. I hope I was clear. :)
I believe you can just use the BETWEEN operator to check that the rent_date is not between your intervals, like this:
SELECT *
FROM myTable
WHERE rent_date NOT BETWEEN '2014-11-12' AND '2014-11-22'
If you are looking to find out the records whose rent and return dates are not in the given range, you can use a query by comparing the dateS as follows:
SELECT * FROM DB WHERE rent_date < '2014-11-12 00:00:00' OR return_date > '2014-11-22 23:59:59'
You need to use a range query.
SELECT foo FROM bar WHERE timestamp BETWEEN timestamp1 AND timestamp2
in my database i use a column named startdate and in the column there are rows with timestamps, looking like: 1410178260
Normally, when i use a datetime field and i want to select all the items with the date of today, i run this query:
$sql = "SELECT id FROM agenda2 WHERE DATE(startdate) >= CURRENT_DATE()";
But now, using the timestamps, i don't know how to make a query that selects all the items inserted today.
Can someone help me with that?
You need to convert to date using the function from_unixtime()
mysql> select FROM_UNIXTIME('1410178260');
+-----------------------------+
| FROM_UNIXTIME('1410178260') |
+-----------------------------+
| 2014-09-08 17:41:00 |
+-----------------------------+
So you may do as
SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()
If you're using unix timestamps then you've got to use
$sql = "SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()";
Consider that both versions can't make use of an index.
see FROM_UNIXTIME
I have this table in mysql
Date one | Date tow |
2012-05-20 | 2012-05-04 | = 16 days
2012-05-12 | 2012-05-08 | = 4 days
= 20 days
and i want to select difference between two dates and then sum all days.
If you should do it in MySQL, you could use DATEDIFF function.
SELECT DATEDIFF(dateone, datetwo) AS d FROM tablename
and then you could aggregate this result the way you want, example
SELECT SUM(DATEDIFF(dateone, datetwo)) AS s FROM tablename
You can do it also in PHP after fetching the dates
You can try the below query. hope it will sort out the issue.
SELECT SUM(DATEDIFF(DATE1,DATE2)), FROM TABLE
You can use the DATEDIFF function in mysql.
SET #runtot:=0;
SELECT DATEDIFF(one,tow) AS diff, (#runtot := #runtot + DATEDIFF(one,tow)) AS runningsum
FROM table
SELECT SUM(DATEDIFF(`Date one`, `Date tow`)) FROM `Table`
See DATEDIFF() and SUM().
Note that in MySQL, you do not need to use GROUP BY in order to be able to use the aggregate function SUM(). In that case, MySQL will regard all rows as a single group.
I'm using mysql and I'm having trouble thinking of a query to count the number of users/visitors for a certain date range. The way that I'm currently doing it is using php, I select the date range and process the data in a for loop and then just count them there. It's actually pretty easy, but the problem is that this method does not work for bigger data of a few million rows. The alternative is to count the distinct values using mysql only and just return a count and not actual data by utilizing the index on the timestamp column. Also, converting the column to a datetime is not an option. Any ideas how I can achieve this?
Here's a sample result set of what I need:
date | count
5-01-13 14
5-02-13 44
5-03-13 23
5-04-13 13
My problem is that I don't know how to group the timestamp column by day.
That should do the trick:
SELECT DATE('datetimecolumn'), COUNT(*)
FROM Table
GROUP BY DATE('datetimecolumn')
You just have to do the same, but instead add a group by clause:
SELECT myDate, count(distinct myField) as cnt
FROM myTable
WHERE myDate BETWEEN ? and ?
GROUP by myDate;
Where the "?" are the dates you use in your original query.