Use of COUNT twice in PHP MySQL Left Join - php

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

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

SELECT COUNT() in a left join table mysql

I am attempting to count the number of new leads and the number of closed leads in any given month. The query below returns the month array correctly, but then only counts 1 if there are any leads or closes in the months, instead of the actual number that happened in that month. Any ideas what I am doing wrong?
query:
$productSql = "SELECT months.monthnum as monthnum, COUNT(contacts.lead_date) as lead_date_month, COUNT(contacts.close_date) as close_date_month, contacts.rep as rep
FROM months
LEFT JOIN contacts
ON months.monthnum = MONTH(contacts.lead_date)
AND months.monthnum = MONTH(contacts.close_date)
AND contacts.compid='$compid'
AND YEAR(contacts.lead_date) = '$year'
AND YEAR(contacts.close_date) = '$year'
AND contacts.rep = '$rep'
GROUP BY months.monthnum
ORDER BY months.monthnum ASC";
$productResult = mysql_query($productSql, $link) or die(mysql_error());
$label[] = $productRow['monthnum'];
$lead[] = $productRow['lead_date_month'];
$close[] = $productRow['close_date_month'];
};
echo "[".json_encode($label).",".json_encode($lead).",".json_encode($close)."]";
result:
[["January","February","March","April","May","June","July","August","September","October","November","December"],
["0","0","0","0","0","0","0","0","1","1","0","0"],
["0","0","0","0","0","0","0","0","1","1","0","0"]]
**Please don't chastise me for using mysql. I am dumping it as soon as this project is finished.
The way you build your JOIN condition you will get results only from records with MONTH(contacts.lead_date) = MONTH(contacts.close_date).
To fix that and to get your actual Count I would suggest splitting the statement into two queries:
SELECT
a.monthnum,
a.lead_date_month, b.close_date_month,
b.close_date_month / a.lead_date_month
FROM
(
-- Count number of new leads
SELECT
months.monthnum as monthnum,
COUNT(*) as lead_date_month,
contacts.rep as rep
FROM
months
LEFT JOIN contacts
ON
months.monthnum = MONTH(contacts.lead_date)
AND contacts.compid='$compid'
AND YEAR(contacts.lead_date) = '$year'
AND contacts.rep = '$rep'
GROUP BY
months.monthnum
ORDER BY
months.monthnum ASC
) as a
JOIN
(
-- Count number of closed leads
SELECT
months.monthnum as monthnum,
COUNT(*) as close_date_month,
contacts.rep as rep
FROM
months
LEFT JOIN contacts
ON
months.monthnum = MONTH(contacts.close_date)
AND contacts.compid='$compid'
AND YEAR(contacts.close_date) = '$year'
AND contacts.rep = '$rep'
GROUP BY
months.monthnum
ORDER BY
months.monthnum ASC
) as b
ON a.monthnum = b.monthnum
And MySQL isn't that bad. But MariaDB is better, you are right :-)

PHP Mysqli how to select data from the last 24hours

I want to get data added in the last 24 hours I already tried with this query but it shows nothing:
$result = $db->query("
SELECT reclamations.* , customers.*
FROM reclamations
LEFT JOIN customers ON reclamations.id_customer = customers.id
ORDER BY reclamations.code
WHERE reclamation_date = DATE_SUB( NOW() - INTERVAL 24 HOUR"));
You currently have a PHP and MySQL syntax error because you having a closing parenthesis the wrong side of your closing double-quote.
How about the following, where the column is more than or equal to now minus 1 day:
$result = $db->query("
SELECT reclamations.* , customers.*
FROM reclamations
LEFT JOIN customers
ON reclamations.id_customer = customers.id
ORDER BY reclamations.code
WHERE reclamation_date >= NOW() - INTERVAL 1 DAY;
");
Try this:
$result = $db->query("
SELECT reclamations.* , customers.*
FROM reclamations
LEFT JOIN customers ON reclamations.id_customer = customers.id
ORDER BY reclamations.code
WHERE reclamation_date BETWEEN DATE_SUB(NOW(), INTERVAL 24 HOUR) and NOW()";

Speeding up SQL loop

I have a nested loop in my PHP code that is taking a long time and wondered of there was a way to speed this up:
$sql = "SELECT * FROM table_1";
$result = mysqli_query($sql);
while($row = mysqli_fetch_array($result)){
$sql2 = "
SELECT count(*)
FROM user_modules
WHERE
begin_ymdhi >= ".$date_from." AND
begin_ymdhi <= ".$date_to." AND
(
completed_ymdhi IS NULL OR
completed_ymdhi = ''
) AND
user_id = ".$row['user_id']." AND
task_id = ".$row['task_id']." AND
module_discon = 'N'
";
}
The outer query gets 1000 rows, the inner has to count across 10,000 rows - the run-time is around 115 seconds ! Is there any way to improve this method, either using a different technique or combined SQL query?
Don't use nested queries, combine them into a single query with a join:
SELECT t1.*, COUNT(u.user_id) ct
FROM table_1 t1
LEFT JOIN user_modules AS u ON u.user_id = t1.user_id AND u.task_id = t1.task_id
AND u.begin_ymdhi BETWEEN '$date_from' AND '$date_to'
AND u.module_discon = 'N'
GROUP BY t1.user_id, t1.task_id
Are the task_id's unique? if so, the most straight forward would be something like:
$sql2 = "
SELECT count(task_id) AS TaskCount
FROM user_modules
WHERE
begin_ymdhi >= ".$date_from." AND
begin_ymdhi <= ".$date_to." AND
(
completed_ymdhi IS NULL OR
completed_ymdhi = ''
) AND module_discon = 'N'
group by user_id
";
$result = mysqli_query($sql2);
SELECT user_modules.user_id, user_modules.task_id, count(*)
FROM user_modules LEFT JOIN table_1 USING (user_id, task_id)
WHERE
begin_ymdhi >= ".$date_from." AND
begin_ymdhi <= ".$date_to." AND
module_discon = 'N' AND
(
completed_ymdhi IS NULL OR
completed_ymdhi = ''
)
GROUP BY user_modules.user_id, user_modules.task_id
Append EXPLAIN before that entire SELECT statement (i.e EXPLAIN SELECT count(*)...) and MySQL will give you a run-down on what the select is doing.
Make sure the begin_ymdhi field is indexed properly. SHOW INDEX FROM table_2 to see.

simple order by clause not working with inner join

this query is simply not ordering results the way i want it to:
$sql = mysql_query("select s.*, p.label, p.number
from `sales` as s
left join `pumps` as p on (p.id = s.id_pump)
where s.the_date >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
order by CAST(s.amount_sale AS int) desc
limit 1") or die(mysql_error());
i am getting a mysql error.
Try casting the VARCHAR to a signed integer:
$sql = mysql_query("select s.*, p.label, p.number
from `sales` as s
left join `pumps` as p on (p.id = s.id_pump)
where s.the_date >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
order by CAST(s.amount_sale AS SIGNED) desc
limit 1");

Categories