match date in mysql and give result - php

I have written query in mysql as
select * from tblstafftasks as tst join tblstafftaskassignees as tsta on tsta.taskid = tst.id join tblstaff as ts on ts.staffid = tsta.staffid where tst.startdate like '2016-07-21' or '2016-07-21' between tst.startdate and tst.duedate and tst.rel_id = 1
My database looks like :
When I have written above query it runs perfect but does not give any result.
Here I have used 2016-07-21 that match with the startdate so it not give any result. So what query should I have to write to get result if it match with the startdate.?
Here i pass date in simple Y-m-d format and in database it store as datetime so help me to solve this query.

It seems like you want to compare a date with DATETIME data types. If you want to compare just with the date of the DATETIME field, do something like this:
SELECT * FROM tblstafftasks AS tst
JOIN tblstafftaskassignees AS tsta ON tsta.taskid = tst.id
JOIN tblstaff AS ts ON ts.staffid = tsta.staffid
WHERE DATE(tst.startdate) <= '2016-07-21'
AND DATE(tst.duedate) >= '2016-07-21'

Related

How do I change date format in SQL SELECT?

I want to change the date format in my SQL calling, but I got the wrong output how to solve this?
SELECT s.id, format(s.TransacDate,'dddd, d MMMM, yyyy') AS formatDate, s.name
FROM sales s
JOIN user u
ON s.No = u.No
WHERE u.logId= 'abcde';
OUTPUT DATE THAT I GET FROM THE QUERY
This is how my DB store the format date 2020-10-21 09:25:10
I want to achieve the result of 21/10/2020 09:25:10
How do I fix my current query in order to achieve the result?
Seems that "format" is the MSSQL function. For MySQL, try date_format:
date_format(s.TransacDate, '%d/%m/%Y %H:%i:%s')
This will be correct answer.
SELECT s.id, format(s.TransacDate.getdate(),'dd/MM/yyyy,hh:mm:ss') AS formatDate, s.name
FROM sales s // --------------------
JOIN user u // ^--------------------- change here
ON s.No = u.No
WHERE u.logId= 'abcde';

when i pass date through variables inside query don't get output

I am facing a very strange issue trying to solve it since last 6 hours but all in vain. When i store date inside the variable the query shows 0 results. But when i store date date directly inside query it gives me output. But if i store date inside variable like below it does not give me any output.
$fromdate="2020-01-08";
$todate='2020-08-14';
And below is a query
"SELECT A1.u_id_fk, A3.full_name, A1.created AS check_in_at, A2.created AS check_out_at, TIMEDIFF(A2.created, A1.created) AS total_time
FROM timesheet
AS A1 INNER JOIN timesheet AS A2 ON A1.u_id_fk = A2.u_id_fk AND DATE(A1.created) = DATE(A2.created)
INNER JOIN users A3 on A1.u_id_fk=A3.u_id
WHERE 1 = 1 AND A1.status = 'Check-In' AND A2.status = 'Check-Out'
AND DATE(A1.created) BETWEEN :fromdate AND :todate
AND DATE(A2.created) BETWEEN :fromdate AND :todate
ORDER BY A1.created DESC"
above query only returns rows when i provide date directly inside query such '2020-01-08' instead of :fromdate and :todate.

Explode data from MySQL field and compare with string

I have a problem with data in MySQL.
I have a column "vacation_period" where I keep data with dates separated by commas eg. "02/10/2015,02/11/2015" and I want to explode commas, month and year. I want to make condition where I can compare month and year sent by POST with exploded data from MySQL field.
Example query:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
ag_vacations.vacation_period = 2015 AND
ag_vacations.vacation_period = 02
order by date_added desc
So try this way:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
((ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/2015\,[0-9]{2}\/[0-9]{2}\/[0-9]{4}' AND
ag_vacations.vacation_period REGEXP '02\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/[0-9]{4}')
OR (ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/2015' AND
ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,02\/[0-9]{2}\/[0-9]{4}'))
order by date_added desc
so if you need to check just first date in that string 02/10/2015,02/11/2015 - you could delete OR part:
OR (ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/2015' AND
ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,02\/[0-9]{2}\/[0-9]{4}')
While a string of dates is not ideal, we sometimes receive data this way. If you need to query this before you have the opportunity to clean up your schema, and if your date formats are consistent, you can query the string with like. In this query, we will concat commas to the beginning and end of the ag_vacations.vacation_period column to make sure we are getting the beginning of the date for month and the end of the date for year:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
concat(ag_vacations.vacation_period,',') like '%/2015,%' AND
concat(',',ag_vacations.vacation_period like '%,02/%'
order by date_added desc
If you just want to find the row, you don't need to explode in php.You can use FIND_IN_SET
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

Assistance with Search MySql DB using date and time range

I have a search page for my users and one of the things I want them to be able to do is search items in our DB using a time range.
Currently, my query looks like this:
select ****
from ****
where created_on >='{$errTimeDayOne}' and created_on <= '{$errTimeDayTwo}'
So, say $errTimeDayOne = 2013-03-24 00:00:00 and $errTimeDayTwo = 2013-04-01 01:00:00 it will search between that range. I'm trying to search between 00:00:00 - 01:00:00 through dates 2013-03-24 - 2013-04-01. Any thoughts or suggestions?
Update:
My query now looks like this:
SELECT call_id, priority_name, caller_name,
cust_name, cust_rep, caller_dept,
DATE_FORMAT(created_on , '%Y-%m-%d') AS created_on,
DATE_FORMAT(updated_on, '%Y-%m-%d') AS updated_on,
source_name, created_by, cat_name, subcat_name
FROM calls INNER JOIN categories ON calls.cat_id =
categories.cat_id INNER JOIN ticket_priority ON
calls.ticket_priority = ticket_priority.priority_id
INNER JOIN ticket_source ON calls.ticket_source =
ticket_source.source_id LEFT OUTER JOIN subcategories
ON calls.subcat_id = subcategories.subcat_id WHERE
created_by = 'brett.little' AND (DATE(created_on)
between '2013-03-01' and '2013-08-01') AND
(TIME(created_on) between '1:00:00' and '16:00:00')
It is displaying results when I leave out '(TIME(created_on) between '1:00:00' and '16:00:00')'. Might this have anything to do with the date_format in the beginning of the query?
You'll have to enhance the clause to check the times and dates separately:
WHERE (DATE(created_on) BETWEEN '2013-03-24' and '2013-04-01')
AND (TIME(created_on) BETWEEN '00:00:00' AND '01:00:00')

how to compare two tables fields name with another value in mysql?

I have two tables
table_school
school_open_time|school_close_time|school_day
8:00 AM | 9:00PM | Monday
10:00 AM | 7:00PM | Wednesday
table_college
college_open_time|college_close_time|college_day
10:00 AM | 8:00PM | Monday
10:00 AM | 9:00PM | Tuesday
10:00 AM | 5:00PM | Wednesday
Now I want to select school_open_time school_close time, college_open_time and college_close_time according to today (means college_day=school_day=today), and also if there is no row for a specific day in any of one table then it display blank field ( LEFT JOIN , I think I can use).
Please suggest me best and optimized query for this.
UPDATE:
if there is no open time and close time for school then college_open_time and college_close_time has to be returned( not to be filled in database,just return) as school_open_time and school_close_time. and there always must be college_open_time and college_close_time for a given day
i m using below query
SELECT college_open_time,college_close_time ,school_open_time,
school_close_time FROM tbl_college
LEFT JOIN tbl_school ON school_owner_id=college_owner_id
WHERE college_owner_id='".$_session['user_id']."' AND
college_day='".date('l',time())."'";
it return single row (left hand having some value and right hand having blank value) when there is no row of a given day in table_school, BUT display seven rows with same value on left hand side(college_open_time, college_close_time) and 6 blank row on right hand side (school_open_time and school_close_time)
i need only one row when both table have a row of a given day
but using above query take only first row of corresponding table_school where school_owner_id is 50(let), it not see the condition that school_day name should be given day
More UPDATE #37Stars
There is a little bit problem also Dear,
datatype of school_close_time and school_open time is TIME type
whereas datatype of college_open_time and college_close_time is VARCHAR type.
i used below code given by you but i modified a bit and i m getting close to result,
but now tell me where i have to write IFNULL in below code segment
IFNULL(TIME_FORMAT()) Or TIME_FORMAT(IFNULL())
SELECT TC.owner_id,college_open_time AS collegeOpen,
college_close_time AS collegeClose,
TIME_FORMAT(school_open_time, '%h:%i %p' ) AS schoolOpen,
TIME_FORMAT(school_close_time, '%h:%i %p' ) AS schoolClose
FROM tbl_college TC
LEFT JOIN tbl_school TS ON TS.owner_id = TC.owner_id
AND TC.college_day = TS.school_day
WHERE college_day = DATE_FORMAT(NOW(),'%W')
Solution
Thanks 37stars, u r genious, thanx for the ideo of IFNULL,
i m writing OPTIMUM AND BEST QUERY
SELECT TC.owner_id,college_open_time AS collegeOpen,college_close_time AS
collegeClose, IFNULL(TIME_FORMAT(school_open_time, '%h:%i %p'),college_open_time)
AS schoolOpen,IFNULL(TIME_FORMAT(school_close_time, '%h:%i %p',college_close_time)
AS schoolClose FROM tbl_college TC LEFT JOIN tbl_school TS
ON TS.owner_id = TC.owner_id AND TC.college_day = TS.school_day
WHERE college_day = DATE_FORMAT(NOW(),'%W')
FROM tbl_storecalendar TS LEFT JOIN tbl_delivery_hours TD
ON TD.store_id = TS.store_id
AND TD.del_day = TS.dayName WHERE dayName = DATE_FORMAT( NOW( ) , '%W' )
You want a FULL OUTER JOIN, but unfortunately MySQL doesn't support this. Luckily, there is a workaround by combining a left join and a right join using UNION ALL:
Update: Changed query to answer OP's updated question.
SELECT
COALESCE(school_day, college_day) AS day,
COALESCE(school_open_time, college_open_time) AS school_open_time,
COALESCE(school_close_time, college_close_time) AS school_close_time,
COALESCE(college_open_time, school_open_time) AS college_open_time,
COALESCE(college_close_time, school_close_time) AS college_close_time
FROM (
SELECT * FROM table_school LEFT JOIN table_college ON school_day = college_day
UNION ALL
SELECT * FROM table_school RIGHT JOIN table_college ON school_day = college_day
WHERE school_day IS NULL
) AS T1
You need to add school_day = college_day to your JOIN clause.
SELECT college_open_time, college_close_time, school_open_time, school_close_time
FROM dbo.tbl_college AS TC
LEFT JOIN dbo.tbl_school AS TS ON TS.owner_id = TC.owner_id
AND TS.school_day = TC.college_day
WHERE TC.owner_id = 1
AND college_day = 'tuesday'
I would recommend changing your DB structure to two tables with the following structure:
table institutions:
institution | institution_id
table times:
institution_id | day | open_time | close_time
You could easily put your two existing tables into the new times table, i.e.
INSERT INTO times(institution, day, open_time, close_time)
SELECT 'School', school_day, school_open_time, school_close_time
FROM table_school
And then getting the query results is easy:
SELECT i.institution, t.open_time, t.close_time
FROM times t
LEFT JOIN institutions i on t.institution_id=i.institution_id
WHERE day='Wednesday'
If you want to accomplish this in one query, you'll have to use UNION to put your results together, like this:
SELECT school_open_time AS openTime,`int` AS school_close_time
FROM table_school WHERE school_day=DATE_FORMAT(NOW(),'%W')
UNION
SELECT college_open_time AS openTime,`int` AS college_close_time
FROM table_college WHERE college_day=DATE_FORMAT(NOW(),'%W');
Where DATE_FORMAT(NOW(),'%W') converts to the current day of the week.
However, this method doesn't seem all that great to me. First of all, it won't handle putting NULL methods into the result set for you in the case of a missing record. You could add some IF/ELSE statements to do this for you, but in all honesty, I'd probably just make two separate queries from your PHP code. That way, you can handle each one specifically and control your results more easily from there.

Categories