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.
Related
mysql table: stats
columns:
date | stats
05-05-2015 22:25:00 | 78
05-05-2015 09:25:00 | 21
05-05-2015 05:25:00 | 25
05-04-2015 09:25:00 | 29
05-04-2015 05:25:00 | 15
sql query:
SELECT MAX(date) as date, stats FROM stats GROUP BY date(date) ORDER BY date DESC
when I do this, I does select one row per date (grouped by date, regardless of the time), and selects the largest date with MAX, but it does not select the corresponding column.
for example, it returns 05-05-2015 22:25:00 as the date, and 25 as the stats. It should be selecting 78 as the stats. I've done my research and seems like solutions to this are out there, but I am not familiar with JOIN or other less-common mysql functions to achieve this, and it's hard for me to understand other examples/solutions so I decided to post my own specific scenario.
This question is asked every single day in SO. Sometimes, it's correctly answered too. Anyway, purists won't like it but here's one option:
Select x.* from stats x join (SELECT MAX(date) max_date FROM stats GROUP BY date(date)) y on y.max_date = x.date;
Obviously, for this to work dates need to be stored using a datetime data type.
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'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.
I'm trying to avoid involving php after this query. I have a table that holds a list of employees and how much time they worked in seconds,date, etc... i want to:
select SUM(`seconds`) between date A and date B group by WEEK(`date`)
that will give me results for each week but now i want to get an average seconds worked per week by using AVG() on the whole result set. How could you accomplish this in one query?
You can use something like this
select sum(total) from (select SUM(`seconds`) as total between date A and date B group by WEEK(`date`)) as tbl1
Hope it help
This will do the trick:
Select AVG(sum_seconds) from (select SUM('seconds') as sum_seconds between date A and date B group by WEEK('date')) as a
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.