How do I change date format in SQL SELECT? - php

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';

Related

combine two tables and sum mysql

i want to join two tables but i can't do it as i want to sum column and get the result between two dates
first table named : vip_allotment_details
allotment_id qty
2 3
2 5
1 2
1 4
the second table name : vip_allotment
id date_from date_to
1 2017-10-1 2017-10-5
2 2017-10-6 2017-10-10
what i want from the query to get me this result
id qty date_from date_to
1 6 2017-10-1 2017-10-5
2 8 2017-10-6 2017-10-10
i will explain the result :
first allotment_id field is linked with id field in second table , the result i want that we can make sum of qty by the two fields (id , allotment_id ) between the date_from and date_to
and here is my try :
$query1 = "
SELECT SUM(qyt) as total
FROM vip_allotment_details
where allotment_id IN ( SELECT id from vip_allotment where date_from >= '$date_1' AND date_to <= '$date_2')
";
In my query the result gets all the sum of qty field with no filter ..
I hope I have explained my problem well .
thanks/.
I'm not try yet, but maybe you can try like this:
SELECT a.id AS id, SUM(qyt) AS qty, date_from, date_to
FROM vip_allotment AS a
LEFT JOIN vip_allotment_details AS b on b.allotment_id = a.id
WHERE a.date_from >= '{thedatestart}' AND a.date_to <= '{thedateend}'
GROUP BY a.id
ORDER BY a.id ASC;
You need to use JOIN. I see you are using IN keyword, which won't work. There can be many ways to solve your problem. One of them is,
select allotment_id, qty, date_from, date_to
from
(select allotment_id, SUM(qty) as qty
from vip_allotment_details group by allotment_id
) at
INNER JOIN
vip_allotment va
ON va.id= at.allotment_id;
I think the following should do what you ask.
SELECT
va.id,
SUM(vad.qyt) AS total,
va.date_from,
va.date_to
FROM vip_allotment_details AS vad
LEFT JOIN vip_allotment AS va ON va.id = vad.allotment_id
GROUP BY vad.allotment_id
Try below.i think you will get your desired result.
select va.id, temp.qty , va.date_from,va.date_to from vip_allotment as va
inner join (select sum(qty) as qty , allotment_id from vip_allotment_details group by `allotment_id`) as temp
ON temp.allotment_id=va.id
where va.date_from >= '$date_1' AND va.date_to <= '$date_2';
If you want more then one result form an aggregate function (SUM, COUNT, AVG, ...) you'll need to use a GROUP BY. Your query isn't that hard, this should do the trick:
SELECT va.id, va.date_from, va.date_to, SUM(vad.qyt) AS qyt
FROM vip_allotment AS va
LEFT JOIN vip_allotment_details AS vad ON vad.allotment_id = va.id
GROUP BY va.id
And as you can see here, this produces the expected result: http://sqlfiddle.com/#!9/707a8/2
If you now want to start adding extra filters (like filter by date), you can just do so by adding a WHERE to the query. Something like this:
...
LEFT JOIN ...
WHERE va.date_from >= "2017-10-06" and va.date_to <= "2018-10-06"
GROUP BY ...
http://sqlfiddle.com/#!9/707a8/6
On a side note, I noticed you are not binding your params in the php part of your code . Do note that this can pose serious security issues, especially if these dates come directly from the user input. I would suggest looking in to PDO to do the actual querying in PHP.
Try this..change your table name and run the query..hopefully it should give the result as your requirement..if not let me know...
select a.id
, sum(b.qty)
, a.date_from
, a.date_to
from table1 a
, table2 b
where a.id = b.allotment_id
group
by b.allotment_id

match date in mysql and give result

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'

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')

SQL- Query to select data using date selected using datepicker

I want to get data from database based on date selected using datepicker.
I am using the following query to get data between selected dated but what i need is like when i select from_date and to_date using datepicker and click a button i want all the data assocoated with the selected date to be displayed. What change should i make in the query (I think after Between). Someone please help me. Thanks in advance. I want to use this with PHP as a server page.
SELECT
EP.Employee_Id,
TD.Training_Id,
TD.Training_Date,
DT.Topic_Name,
EP.Employee_Name
FROM
`training_details` TD
INNER JOIN `domain_topics` DT ON DT.Domain_Id=TD.Domain_Id
INNER JOIN `trainer_details` TRD ON TRD.Training_Id = TD.Training_Id
INNER JOIN `employee_profile` EP ON TRD.Trainer_Id = EP.Employee_Id
WHERE
TD.Training_Date BETWEEN '2012-12-01' AND '2012-12-31';
This might help: If you are using POST method:
SELECT EP.Employee_Id, TD.Training_Id, TD.Training_Date, DT.Topic_Name, EP.Employee_Name
FROM `training_details` TD
INNER JOIN `domain_topics` DT ON DT.Domain_Id=TD.Domain_Id
INNER JOIN `trainer_details` TRD ON TRD.Training_Id = TD.Training_Id
INNER JOIN `employee_profile` EP ON TRD.Trainer_Id = EP.Employee_Id
WHERE TD.Training_Date >= '$_POST[from_date]' AND TD.Training_Date <= '$_POST[to_date]';
OR as in your query WHERE clause be like this:
WHERE TD.Training_Date BETWEEN $_POST[from_date] AND $_POST[to_date]
If you are using GET method, it will be:
WHERE TD.Training_Date BETWEEN $_GET[from_date] AND $_GET[to_date]

Grouping records from MySql

I've got two tables in MySql
1 (Staff): Id/Name/SecondName
2 (fee):
Id/StaffId/Date(yyyy-mm-dd)/HoursWorked(hh:mm)/fee(int)/workType
There is also a script adding records to fee table.
I'm trying to group data in php to create html table like:
Name, Second Name | January 2009 | 123:45 hours | 2100,00 USD
February 2009...
March 2009 ....
Next person... etc.
So generally I'm trying to sum fee and hours in specific month and print a report from database...
And I need some advice/help... What is the bast way to create table like this?
Maybe something like this? Not tested though...
SELECT s.Name, s.SecondName, CONCAT(DAYOFMONTH(f.Date),', ',YEAR(f.Date)),
SUM (f.HoursWorked), SUM(f.Fee)
FROM Staff s
JOIN Fee f ON f.StaffId = s.Id
GROUP BY s.Id, YEAR(f.Date), MONTH(f.Date)
Edit: Ofcourse you need to group on s.Id...
That's not the best way, but if you want to do it with one query (it's easy to export to Excel):
SELECT
s.Name,
s.SecondName,
DATE_FORMAT('%M %y', f.`Date`),
SEC_TO_TIME( SUM( TIME_TO_SEC( `HoursWorked` ) ) ) as TotalHours,
sum(fee) AS TotalFee
FROM
Staff AS s
INNER JOIN fee AS f on s.id = f.StaffId
WHERE
1
GROUP BY s.id, YEAR(f.`Date`), MONTH(f.`Date`)
You cal also query stuff:
// that's not a real function, just get all Staff into $staff
$staff = QueryRows(SELECT * FROM Staff);
and then query fee:
foreach($staff as $s){
// use this query to query statistics
SELECT * FROM fee
WHERE StaffId = $s['id']
GROUP BY StaffId, YEAR(f.`Date`), MONTH(f.`Date`)
}
I'm not 100% sure about this being perfect but it should definitely point you in the right direction. The AVG() function calls might be unnecessary.
SELECT
Name,
SecondName,
SUM(fee.HoursWorked) as HoursWorked,
SUM(fee.fee) as fee,
YEAR(AVG(fee.Date)) as year,
MONTH(AVG(fee.Date)) as month
FROM Staff
JOIN fee ON staff.id = fee.staffid
ORDER BY fee.Date
GROUP BY staff.id, YEAR(fee.Date), MONTH(fee.Date)

Categories