I have a problem about getting all the data's in one column and will add it by user id.
I Retrieve the data's by this query
SELECT uid, date, timein, timeout,TIMEDIFF(timein, '08:00:00') as DATE_DIFF
FROM sample_tbl
WHERE date >= '2015-11-01'
AND date <= '2015-11-15'
AND uid IN (32,61,53,54,62,57,55,58,34,60,63,59)
AND timein > '08:00:00'
The Output
My Problem is How can I add them? for example uid = 32 should be
uid = 32 and DATE_DIFF = "00:11:00"
I also tried Group By uid and SUM() or is it possible?
Ok iv'e read about SUM() function and add this to my query SUM(minute(TIMEDIFF(timein, '08:00:00'))) and Group it by ID
SELECT uid, date, timein, timeout,SUM(minute(TIMEDIFF(timein, '08:00:00'))) as Total_late
FROM sample_tbl
WHERE date >= '2015-11-01'
AND date <= '2015-11-15'
AND uid IN (32,61,53,54,62,57,55,58,34,60,63,59)
AND timein > '08:00:00'
GROUP BY uid
Related
I have the a sql table payments with the following columns:
id|name|amount|reference_number|payment_date|status
And here is some values
1|John Doe|10.00|123456G|2019-02-21 08:00:21|confirmed
2|John Doe|10.00|FHFHgJJ|2019-02-21 10:05:50|pending
3|John Doe|10.00|57GHHFG|2019-02-22 12:10:32|pending
4|John Doe|10.00|GHTYNHJ|2019-02-22 09:52:26|confirmed
5|John Doe|10.00|123G456|2019-02-23 12:22:45|confirmed
6|John Doe|10.00|J123456|2019-02-23 13:00:21|pending
Now I need to fetch select the data based on payment_date and status where sometimes the payment_date can be a range or the same date.
Scenario 1:
SELECT *
FROM payments
WHERE payment_date >= '2019-02-21' AND payment_date <= '2019-02-21' AND status='confirmed'
ORDER BY id DESC
Should return:
1|John Doe|10.00|123456G|2019-02-21 08:00:21|confirmed
Screnario 2:
SELECT *
FROM payments
WHERE payment_date >= '2019-02-21' AND payment_date <= '2019-02-23' AND status='confirmed'
ORDER BY id DESC
Should return:
1|John Doe|10.00|123456G|2019-02-21 08:00:21|confirmed
4|John Doe|10.00|GHTYNHJ|2019-02-22 09:52:26|confirmed
5|John Doe|10.00|123G456|2019-02-23 12:22:45|confirmed
Any work around this?
As #strawberry suggested use time as well with date to fetch the data
SELECT *
FROM payments
WHERE payment_date >= '2019-02-21 00:00:00' AND payment_date <= '2019-02-21
23:59:59' AND status='confirmed'
ORDER BY id DESC
You can use + interval 1 day for the end date and change <= to <, like this:
SET #startdate = '2019-02-21';
SET #enddate = '2019-02-21';
SELECT *
FROM payments
WHERE
payment_date >= #startdate
AND
payment_date < #enddate + interval 1 day
AND
status='confirmed'
ORDER BY id DESC;
See the demo
TABLE
Table:
Id Date
1 01-10-15
2 01-01-16
3 01-03-16
4 01-06-16
5 01-08-16
Given two dates startdate 01-02-16 and enddate 01-05-16. I need to get the data from the table such that it returns all data between the closest past date from startdate and closest future date from enddate including the two dates. So the result will look like this.
Result:
Id Date
2 01-01-16
3 01-03-16
4 01-06-16
What I am doing
What I am doing now is fetching the whole data and removing from the array results less than closest fromdate and greater than closest enddate
What I want
What I want is to do this in query itself so that I don't have to fetch the whole data from table each time.
If you column's type is date, use union can do it:
(select * from yourtable where `date` <= '2016-01-02' order by `date` desc limit 1)
-- This query will get record which is closest past date from startdate
union
(select * from yourtable where `date` => '2016-01-05' order by `date` asc limit 1)
-- This query will get record which is closest future date from enddate
union
(select * from yourtable where `date` between '2016-01-02' and '2016-01-05')
Demo Here
Imaging your date is in YYYY-mm-dd
## get rows within the dates
SELECT * FROM tab WHERE ymd BETWEEN :start_date AND :end_date
## get one row closest to start date
UNION
SELECT * FROM tab WHERE ymd < :start_date ORDER BY ymd DESC LIMIT 1
## get one row closest to end date
UNION
SELECT * FROM tab WHERE ymd > :end_date ORDER BY ymd LIMIT 1
Try this
Select *
From
dTable
Where
[Date]
Between
(Select
Max(t1.Date)
From
dTable t1
Where
t1.date <startdate) And
(Select
Min(t2.Date)
From
dTable t2
Where
t2.date >enddate)
If Date is String, STR_TO_DATE and DATEDIFF can be used here.
SELECT id, Date
FROM tab
where
STR_TO_DATE(Date, '%d-%m-%y') BETWEEN('2016-02-01')AND('2016-05-01')
or
id = (SELECT id FROM tab
where STR_TO_DATE(Date, '%d-%m-%y') > '2016-05-01'
ORDER BY DATEDIFF(STR_TO_DATE(Date, '%d-%m-%y'), '2016-05-01') Limit 1)
or
id = (SELECT id FROM tab
where STR_TO_DATE(Date, '%d-%m-%y') < '2016-02-01'
ORDER BY DATEDIFF('2016-02-01', STR_TO_DATE(Date, '%d-%m-%y')) Limit 1)
I would like to get the total number of entries, upon 2 or more conditions.
Yet is seems as if mysql is ignoring the 'AND' clause in the query.
edit:
I want to get number of calls made by user with id=97, while the date is between starttime and stoptime, and prefix is like *us*
Example:
select count(*)
from calls
where id = 97
and starttime >= '2012-06-11'
and stoptime >= '2012-06-12'
and prefix like '%us%'
This gives me total amount of calls from id=97, while ignoring the rest of the conditions
I'm guessing that you want the second condition on the date should be <=:
select count(*)
from calls
where id = 97
and starttime >= '2012-06-11'
and stoptime <= '2012-06-12'
and prefix like '%us%';
You should also know that if you are storing times in the date time field along with the date, the you might really want:
select count(*)
from calls
where id = 97
and date(starttime) >= '2012-06-11'
and date(stoptime) <= '2012-06-12'
and prefix like '%us%';
or better yet:
select count(*)
from calls
where id = 97
and starttime >= '2012-06-11'
and stoptime <= '2012-06-13'
and prefix like '%us%';
I think you meant to do something like
select count(*)
from calls
where id = 97
and (starttime >= '2012-06-11' and stoptime <= '2012-06-12')
and prefix like '%us%'
I have a table
id user Visitor timestamp
13 username abc 2014-01-16 15:01:44
I have to 'Count' total visitors for a 'User' for last seven days group by date(not timestamp)
SELECT count(*) from tableA WHERE user=username GROUPBY __How to do it__ LIMIT for last seven day from today.
If any day no visitor came so, no row would be there so it should show 0.
What would be correct QUERY?
There is no need to GROUP BY resultset, you need to count visits for a week (with unspecified user). Try this:
SELECT
COUNT(*)
FROM
`table`
WHERE
`timestamp` >= (NOW() - INTERVAL 7 DAY);
If you need to track visits for a specified user, then try this:
SELECT
DATE(`timestamp`) as `date`,
COUNT(*) as `count`
FROM
`table`
WHERE
(`timestamp` >= (NOW() - INTERVAL 7 DAY))
AND
(`user` = 'username')
GROUP BY
`date`;
MySQL DATE() function reference.
Try this:
SELECT DATE(a.timestamp), COUNT(*)
FROM tableA a
WHERE a.user='username' AND DATEDIFF(NOW(), DATE(a.timestamp)) <= 7
GROUP BY DATE(a.timestamp);
i think it's work :)
SELECT Count(*)
from table A
WHERE user = username AND DATEDIFF(NOW(),timestamp)<=7
Hello i have 2 tables rooms and bookings but my table sturcture little bit different,
in bookings table there are roomid,date,status for example:
101,2012-12-10,0
101,2012-12-11,0
101,2012-12-12,1
101,2012-12-13,0
102,2012-12-10,0
102,2012-12-11,0
102,2012-12-12,0
and i would like to find available rooms between 2012-12-10 and 2012-12-13
according this request only room 102 should be return.
i've tried
SELECT id
FROM status
WHERE status='0'
AND date between '2012-12-10' AND '2012-12-13'
GROUP BY id
it doesn't work because even find only one available row it returns true for 101
so 101 is available for 2012-12-11 then showing like available but not okay for our data range.
This will select all rooms that have no row with status=1 in the interval you select. Notice that I am not using between: if status=1 on 13th of December, the room is still available in the interval, that's why I'm using >= and <:
SELECT roomid
FROM status
WHERE `date` >= '2012-12-10'
AND `date` < '2012-12-13'
GROUP BY roomid
HAVING sum(status.status=1)=0
If there could also be some missing days in your table, and if that means that the room is not booked but also not available, you could also use this query:
SELECT roomid
FROM status
WHERE `date` >= '2012-12-10'
AND `date` < '2012-12-13'
AND status=0
GROUP BY roomid
HAVING count(*)=DATEDIFF('2012-12-13', '2012-12-10')
that checks that the number of days in the interval is equal to the number of rows with status=0.
If the date has a time associated with it you need to do something like this:
SELECT id
FROM status
WHERE status='0'
AND date between '2012-12-10 00:00:00' AND '2012-12-13 23:59:59'
GROUP BY id
You can also use Date(date) instead of including times, but this might be less efficient.
You should be able to use the following:
SELECT roomid
FROM status s1
WHERE status='0'
AND date between '2012-12-10' AND '2012-12-13'
and not exists (select roomid
from status s2
where status='1'
AND date between '2012-12-10' AND '2012-12-13'
and s1.roomid = s2.roomid)
GROUP BY roomid
See SQL Fiddle with demo
Try this:
SELECT rd.*
FROM room_details rd
INNER JOIN (SELECT DISTINCT roomid FROM bookings
WHERE roomid NOT IN (SELECT roomid FROM bookings
WHERE STATUS =0 AND DATE BETWEEN '2012-12-10' AND '2012-12-13')
) AS a ON rd.roomid = a.roomid
The SQL BETWEEN condition will return the records where expression is within the range of value1 and value2 (inclusive). So row 101,2012-12-10,0 should be returned for above mentioned query.
Try this:
SELECT roomid
FROM status
WHERE 'date_from' <= "2012-12-10" AND 'date_to' > "2012-12-10" AND 'status'= 0;