SQL between date today and beyond - php

I am making a Query for a project that I am working on.
I have to make a query that shows me the dates from Today and the dates that will still come. At the moment my Query is this:
$query = "SELECT * FROM systeem
RIGHT JOIN vestiging ON vestiging.id = systeem.vestigingID
LEFT JOIN systeemMeldkamer ON systeemMeldkamer.systeemID = systeem.id
LEFT JOIN meldkamers ON meldkamers.id = systeemMeldkamer.meldkamerID
LEFT JOIN systeemContract ON systeemContract.systeemID = systeem.id
LEFT JOIN onderhoudsLog ON onderhoudsLog.systeemContractID = systeemContract.id
LEFT JOIN contracten ON contracten.ident = systeemContract.contractIDENT
WHERE onderhoudsLog.onderhoudsDatum LIKE '2017%'
ORDER BY onderhoudsLog.onderhoudsDatum ASC";
This Query will give me all dates that look like "2017%".
Instead I want to get the date of today and every date that will come after the date of today:
today it is : 2018-03-05
The result that I want to get: 2018-03-05, 2018-03-06, 2018-03-07
Tomorrow It is: 2018-03-06 And then it has to search for all dates after 2018-03-06
I hope any one can help with my query! Don't mind the "LEFT JOINS", it is because I have to go through 6 tables so I can match the date for maintenance with the number if our customer.

For MySQL, you can use below query. Add where clause like: WHERE onderhoudsLog.onderhoudsDatum >= CURDATE()
Assuming that onderhoudsDatum field is datetime datatype
$query = "SELECT * FROM systeem
RIGHT JOIN vestiging ON vestiging.id = systeem.vestigingID
LEFT JOIN systeemMeldkamer ON systeemMeldkamer.systeemID = systeem.id
LEFT JOIN meldkamers ON meldkamers.id = systeemMeldkamer.meldkamerID
LEFT JOIN systeemContract ON systeemContract.systeemID = systeem.id
LEFT JOIN onderhoudsLog ON onderhoudsLog.systeemContractID = systeemContract.id
LEFT JOIN contracten ON contracten.ident = systeemContract.contractIDENT
WHERE onderhoudsLog.onderhoudsDatum >= CURDATE()
ORDER BY onderhoudsLog.onderhoudsDatum ASC";

You could try this code
$today = date('Y-m-d');
$query = "SELECT * FROM systeem
RIGHT JOIN vestiging ON vestiging.id = systeem.vestigingID
LEFT JOIN systeemMeldkamer ON systeemMeldkamer.systeemID = systeem.id
LEFT JOIN meldkamers ON meldkamers.id = systeemMeldkamer.meldkamerID
LEFT JOIN systeemContract ON systeemContract.systeemID = systeem.id
LEFT JOIN onderhoudsLog ON onderhoudsLog.systeemContractID = systeemContract.id
LEFT JOIN contracten ON contracten.ident = systeemContract.contractIDENT
WHERE onderhoudsLog.onderhoudsDatum => $today
ORDER BY onderhoudsLog.onderhoudsDatum ASC";
You get the date using PHP's date() function, format it as a MySql date string and then use that on the query:
WHERE onderhoudsLog.onderhoudsDatum => $today

is the onderhoudsLog.onderhoudsDatum date field then use '>=CURRENT_DATE' Instead 'like "2017%"' and order by onderhoudsLog.onderhoudsDatum

Related

How to get the latest record by date and not the first from a table?

In my query i get the first record i olje table when i group by frl_nr. There are many records with the same frl_nr i the olje table. I want to get the latest record from the olje table by date column. Her is my search string:
$sql = "SELECT *
FROM frl_sok
INNER JOIN olje ON frl_sok.id_nr = olje.id_nr
WHERE frl_sok.kunde_nr = '$kunde'
AND frl_sok.jobb_nr = '$jobb_nr'
GROUP BY frl_sok.frl_nr DESC";
What is the solution?
Both of my tables are here
You should use row_number() rather than trying to group by.
Try this:
select * from (
SELECT *,
row_number() over (partion by frl_sok.frl_nr order by date desc) rn
FROM frl_sok
INNER JOIN olje ON frl_sok.id_nr = olje.id_nr
WHERE frl_sok.kunde_nr = '$kunde'
AND frl_sok.jobb_nr = '$jobb_nr'
) q where rn=1"
Here is the solution I came up with and it works.
$sql = "SELECT a.id_nr, a.frl_nr, a.maskin_id, a.tilstand_olje, b.date FROM frl_sok a INNER JOIN olje b ON a.id_nr = b.id_nr INNER JOIN (SELECT id_nr, MAX(date) Max_Date FROM olje GROUP BY id_nr) c ON b.id_nr = c.id_nr AND b.date = c.Max_date WHERE a.kunde_nr = '$kunde' AND a.jobb_nr = '$jobb_nr'";

How to remove duplicate values from an sql query

I want to know how to remove duplicate values from the output of an sql query.
This is the sql query:
$query = "SELECT useraccount.Username, tariff.Name as tariffs,
energyconsumption.ElecEnergy, useraccount.Username as User
FROM useraccount
INNER JOIN tariff
ON useraccount.tariffs = tariff.id
INNER JOIN energyconsumption
ON energyconsumption.User = useraccount.id
WHERE Date = CURRENT_DATE";
This is the output of that query:
{"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"2000"},
{"Username":"absc868","TariffName":"s1","ElecConsump":"1900"}]}
As you can see, the query filters out data where the data matches todays date. We have 2 outputs for the same user. The value of the tariff name and username are the same,but the energy consumption value is different which is fine.
I want to achieve the following output:
= {"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"2000 +1900"}
= {"results":[{"Username":"absc868","TariffName":"s1","ElecConsump":"3900"}
Could someone point me to the direction in how I can achieve this?
Thank you in advance to those who read the post and contributed!
You can use the group_concat function:
$query = "SELECT useraccount.Username, tariff.Name as tariffs,
GROUP_CONCAT(energyconsumption.ElecEnergy SEPARATOR ' +')
FROM useraccount
INNER JOIN tariff
ON useraccount.tariffs = tariff.id
INNER JOIN energyconsumption
ON energyconsumption.User = useraccount.id
WHERE Date = CURRENT_DATE
GROUP BY useraccount.Username, tariff.Name";
You should use a sum and a group by
$query = "SELECT useraccount.Username as Username, tariff.Name as TariffName,
sum(energyconsumption.ElecEnergy) as ElecConsump
FROM useraccount
INNER JOIN tariff
ON useraccount.tariffs = tariff.id
INNER JOIN energyconsumption
ON energyconsumption.User = useraccount.id
WHERE Date = CURRENT_DATE
GROUP BY useraccount.Username, tariff.Name as tariffs";
(you have some difference between table column name alias and object attribute name )

How do I change this nested php/mysql query into 1 more efficient query?

I’m looking for some help making my php/MySQL code more efficient. At the moment I’ve got a nested SQL statement in my PHP code which is taking forever to run. I know there is a more efficient way and doing the query in one statement but I'm struggling work out how to do it.
Basically, I have 2 tables. ‘customers’ and ‘purchases’. I want to run through the ‘customers’ table and count/display how many purchases they have made from the ‘purchases’ table.
This is my php/mysql code:
$sql = "SELECT CustomerID, Username, Active FROM customers WHERE AND Active = 'Y'";
$result = mysql_query($sql,$connection);
while ($myrow = mysql_fetch_array($result)) {
$sql2 = "SELECT COUNT(ID) FROM purchases WHERE AND (CustomerID = $myrow[CustomerID] AND (Date BETWEEN $date1 AND $date2) )
$result2 = mysql_query($sql2,$connection);
$TotalPurchases = mysql_result($result2,0,”count(ID)”);
}
Which outputs:
MrSmith: 10
MrsGreen: 4
MrGrey: 1
MissDonna: 0
I could probably turn it into 1 query if it wasn't for the variables $date1 and $date2 (Date BETWEEN $date1 AND $date2) which is calculated in php. Any advice on how to make this a more efficient query?
Thanks
Carlos
Please try this query,
SELECT c.CustomerID, Username, Active , COUNT(p.CustomerID)
FROM customers as c
left join purchases as p ON c.CustomerID = p.CustomerID
AND Date BETWEEN $date1 AND $date2
group by p.CustomerID
I think this can help you.
SELECT c.CustomerID, c.Username, c.Active , COUNT(*)
FROM customers as c
join purchases as p ON c.CustomerID = p.CustomerID
AND p.Date BETWEEN $date1 AND $date2
group by p.CustomerID
Notes: JOIN, not LEFT JOIN. COUNT(*).
purchases should have compound INDEX(Date, CustomerID).
This might be even faster (Edited):
SELECT c.CustomerID, c.Username, c.Active , p.ct
FROM ( SELECT CustomerID, COUNT(*) ct FROM purchases
WHERE Date BETWEEN $date1 AND $date2
GROUP BY CustomerID ) p
JOIN customers AS c ON c.CustomerID = p.CustomerID

How to format date in Query Statement Mysql?

I'm using php with Mysql and I need to run a specific query when user selects month and year in a date_field.
This query returns all records created by the month/year selected by user and I don't want relate days in a clause where inside the select.
Since I need only records from month/year select, is there any ways how can I define inside the select only month/year?
Here is my Mysql statement with variable selected by user
There are two tables related with inner join so I can 'grab' records with Date:
$quer_mesano = "Select b.name AS Canal
from canal_canalvenda b
inner join meta_meta c On c.canal_canalvenda_id_c = b.id
And c.deleted = 0
where c.periodo = '$month_year' //Here is the date value I just need to put in month/year
And b.deleted = 0
Group by Canal order by 1";
Can any one help me?
This is very easy. Use DATE_FORMAT. Just change the Sql statement like this:
$quer_mesano = "Select b.name AS Canal
from canal_canalvenda b
inner join meta_meta c On c.canal_canalvenda_id_c = b.id
And c.deleted = 0
where DATE_FORMAT( c.periodo, '%Y-%m' ) = '$month_year' //Just put DATE_FORMAT
And b.deleted = 0
Group by Canal order by 1";
DATE_FORMAT() you can use as shown below
$quer_mesano = "Select b.name AS Canal
from canal_canalvenda b
inner join meta_meta c On c.canal_canalvenda_id_c = b.id
And c.deleted = 0
where DATE_FORMAT( c.periodo, '%Y/%m' ) = '$month_year'
And b.deleted = 0
Group by Canal order by 1";
For month you have different types -
%M Month name
%m Month, numeric (00-12)
and for year -
%Y Year, four digits
%y Year, two digits
I convert the month year to YYYY-MM formate where it can be compared with the db.
$quer_mesano = "Select b.name AS Canal
from canal_canalvenda b
inner join meta_meta c On c.canal_canalvenda_id_c = b.id
And c.deleted = 0
where DATE_FORMAT( c.periodo, '%Y-%m' ) = '" . date("Y-m", strtotime($month_year)) . "' //Here is the date value I just need to put in month/year
And b.deleted = 0
Group by Canal order by 1";

Use of COUNT twice in PHP MySQL Left Join

I have the following COUNT functionality working successfully in this query. I'd like to issue the exact same COUNT/Left Join functionality on another table (same format) called HTG_ScheduledActual
This works:
$query_WADAHTG_TechProps = "SELECT
HTG_TechProps.EmpNumber, HTG_TechProps.EmpFirstName, HTG_TechProps.EmpLastName, HTG_TechProps.Veh_Number,
COUNT(HTG_ScheduleRequest.ID) AS current_job FROM HTG_TechProps
LEFT JOIN HTG_ScheduleRequest ON HTG_TechProps.EmpNumber = HTG_ScheduleRequest.SSR
AND (HTG_ScheduleRequest.ScheduleDateOriginal = CURDATE()
OR HTG_ScheduleRequest.ScheduleDateCurrent = CURDATE()
OR HTG_ScheduleRequest.ScheduleDateExact = CURDATE() ) GROUP BY HTG_TechProps.EmpNumber
ORDER BY HTG_TechProps.EmpNumber ASC
";
I try to insert a second COUNT using AND and it breaks the query. I have bad feeling I can't issue two COUNT in the same query? How is the best way to overcome this?
$query_WADAHTG_TechProps = "SELECT
HTG_TechProps.EmpNumber, HTG_TechProps.EmpFirstName, HTG_TechProps.EmpLastName, HTG_TechProps.Veh_Number,
COUNT(HTG_ScheduleRequest.ID) AS current_job FROM HTG_TechProps
LEFT JOIN HTG_ScheduleRequest ON HTG_TechProps.EmpNumber = HTG_ScheduleRequest.SSR
AND (HTG_ScheduleRequest.ScheduleDateOriginal = CURDATE()
OR HTG_ScheduleRequest.ScheduleDateCurrent = CURDATE()
OR HTG_ScheduleRequest.ScheduleDateExact = CURDATE() ) GROUP BY HTG_TechProps.EmpNumber
AND COUNT(HTG_ScheduleActual.ID) AS actual_job FROM HTG_TechProps
LEFT JOIN HTG_ScheduleActual ON HTG_TechProps.EmpNumber = HTG_ScheduleActual.SSR
AND (HTG_ScheduleActual.ScheduleDateOriginal = CURDATE()
OR HTG_ScheduleActual.ScheduleDateCurrent = CURDATE()
OR HTG_ScheduleActual.ScheduleDateExact = CURDATE() ) GROUP BY HTG_TechProps.EmpNumber
ORDER BY HTG_TechProps.EmpNumber ASC
";

Categories