I want do a logic that the same date and the time between start and end not get overlaped.
I have 3 separate column for date and start_time and end_time for a venue.
User should select a particular date and time slot. He can select multiple but user can not select overlapping time between selected once.
I have selected the date and time slot. Below is example
In DB table record => 2021-10-22(date) 10:00AM(start_time) 13:30PM(end_time)
User Can select => 2021-10-22(date) 14:00PM(start_time) 16:00PM(end_time)
User Can Not select => 2021-10-22(date) 09:00AM(start_time) 11:30AM(end_time)
User Can Not select => 2021-10-22(date) 13:00PM(start_time) 15:00PM(end_time)
User Can Not select => 2021-10-22(date) 10:00AM(start_time) 13:00PM(end_time)`
I am checking with query.
"SELECT vol_roles.id AS vol_role_id, vol_roles.date,
vol_roles.start_date, vol_roles.end_date,
vol_role_assign.id AS vol_assigned_id,
CONCAT(vol_roles.date, ' ', vol_roles.start_date) as start_time,
CONCAT(vol_roles.date, ' ', vol_roles.end_date) as end_time,
volunteer_role_assign.volunteer_id
FROM vol_roles
JOIN vol_role_assign ON vol_roles.id=vol_role_assign.role_id
WHERE vol_roles.is_active=1
AND vol_role_assign.is_active=1
AND volunteer_role_assign.volunteer_id=:volunteer
AND (
cast(CONCAT(vol_roles.date, ' ', vol_roles.start_date) as datetime)
NOT BETWEEN :start_date_time AND :end_date_time
AND (cast(CONCAT(vol_roles.date, ' ', vol_roles.end_date) as datetime)
NOT BETWEEN :start_date_time AND :end_date_time)
)";
Related
My Model function is as below:
function get_newJoineesList($limit=''){
$SQL ="
SELECT userid as uid, CONCAT( firstname, ' ', lastname ) AS name,profileimage, joining_date, role as designation
FROM pr_users_details
INNER JOIN pr_users ON pr_users.id = pr_users_details.userid
LEFT JOIN pr_userroles ON pr_userroles.id=pr_users.userroleid
WHERE joining_date > DATE_SUB( NOW()-1 , INTERVAL 15 DAY )
ORDER BY pr_users_details.id DESC";
if($limit!=''){
$SQL.=" LIMIT ".$limit;
}
$query = $this->db->query($SQL);
$return = $query->result_array();
return $return;
}
I want to get all the results from 15 days ago to now i.e if today is 15th then retrieve all the joining date that falls between 1-15th. But somehow the above query does not work. I am also restricted to use only varchar(15) type as changing this will affect other modules.
snapshot from db2:
snapshot from db1:
The problem is that the way you store the dates do not correspond to how mysql recognizes date literals, therefore the date calculations will be incorrect. You must use the str_to_date() function to convert the strings to valid dates:
SELECT userid as uid, CONCAT( firstname, ' ', lastname ) AS name,profileimage, joining_date, role as designation
FROM pr_users_details
INNER JOIN pr_users ON pr_users.id = pr_users_details.userid
LEFT JOIN pr_userroles ON pr_userroles.id=pr_users.userroleid
WHERE str_to_date(joining_date,'%d-%c-%Y') > DATE_SUB( NOW()-1 , INTERVAL 15 DAY )
ORDER BY pr_users_details.id DESC
Also, I would consider using curdate() instead of now() because you deal with dates only, not times. Using now() may have interesting side effects of excluding dates that are exactly 15 days ago because of the time component.
first of all i want to extract dates of the relevant month from the database.for that i have used following query and i had generated the following output.
code
SELECT DISTINCT date as tot FROM attendance WHERE date LIKE '%2016-06%'
output
then i need to get the present employees for that dates from the database.but when i try that, it gives me total for one date only.how can i over come this.Here is my code.
SELECT DISTINCT date as days, COUNT(DISTINCT employee_id) as tot FROM attendance WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
You need to use group by. mysql just returns an arbitrary value when you omit fields from the select list that are not included in aggregate functions. Then you can remove distinct:
SELECT date as days, COUNT(DISTINCT employee_id) as tot
FROM attendance
WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
GROUP BY date
Per comment, what data type is your date field? Assuming it's a date, you could remove like and use year and month:
AND Month(date) = 6 AND Year(date) = 2016
I want to fetch data weekwise for the particular month.
ex: if i select may month . i need count of first week,count of second week ,count of third week and so on
use the following query.
select
concat(year(date_format(timefield, '%Y-%m-%d')), ' week ', substring_index(dayofyear(date_format(timefiels, '%Y-%m-%d'))/7+1, '.', 1)) as time,
sum(sales) as sales
from
account
where
timefield>='2014-05-01 00:00:00'
and timefield<='2014-05-31 23:59:59'
group by
time;
I have form with 4 fields namely(start month, start year, end month, end year)
and MySQL table structure is like id, customer id, amount, month, year
Now, I need to display the rows with matching condition as between the start month and year and end month and year.
I tried this query
select id,customer id,concat(month,'-',year) as d1 from payroll where
empid='$_POST[emp_id]' and (STR_TO_DATE(d1,'%m-%Y') between
STR_TO_DATE('$_POST[fmonth]-$_POST[fyear]','%m-%Y') and
STR_TO_DATE('$_POST[tmonth]-$_POST[tyear]','%m-%Y'))
Please advise....
Assuming the columns year and month and the 4 form fields are integers, this would work:
SELECT id
, customer id
, CONCAT(month, '-', year) AS d1
FROM payroll
WHERE empid = '$_POST[emp_id]'
AND (year, month) >= ( '$_POST[fyear]', '$_POST[fmonth]' )
AND (year, month) <= ( '$_POST[tmonth]', '$_POST[tyear]' )
You should probably take care of those $_POST[] before sending them to the database for security reasons (SQL injection).
A compound index on (empid, year, month) would help performance.
I have a database table that is full of transactions transactionIDs and datetime fields.
As you would guess, the datetime field and transactionIDs are constantly increasing and the balance field is either increasing / staying the same / or decreasing.
I would like to extract the highest transactionID and its corresponding balance at the end of every day.
Thank you in advance for your time and assistance.
Sample Table Format:
transactionID|date (datetime)|amount|balance|
SELECT
t1.`date`,
t1.transactionID,
t2.balance
FROM
(
SELECT
`date`,
MAX(transactionID) AS `transactionID`
FROM
table
GROUP BY
DATE(`date`)
) t1
INNER JOIN
table t2
ON
t2.transactionID = t1.transactionID
ORDER BY
t1.`date`
I suggest you that insert unix_timestamp as your date field on your table.and use this query to fetch heighest transactionID :
$query = 'SELECT MAX (transactionID) FROM [ TABLE NAME ] WHERE [ DATE FIELD NAME ] BETWEEN' . strtotime("today 23:59"). ' AND ' . strtotime("today")
Try this:
SELECT balance FROM table WHERE table.created > TODAY() ORDER BY transactionID DESC LIMIT 1
table.created is the column containing the creation date of the record,
This query should give you the transactionID for just the current day. Do you need it for an interval of days? - in that case, the following query should do it
SELECT balance FROM
(SELECT transactionID, balance FROM table ORDER BY transactionID DESC)
GROUP BY DAYOFYEAR(table.created)