having a little trouble, how could I alter this mysql query so that if there are rows in the member_results table it will run the query below but if there is none then pretty much run the query below just without the SUMS() and the joins with the member_results table. I am guessing it can be done with the IF statement in mysql I just have no clue really how to implement with the query below.
Any help is much appreciated.
$result = mysql_query(" SELECT m.member_id, m.teamname,
Sum(Case When r.track_id = '$chosentrack' -1 AND r.track_id >= l.start_race
Then total_points Else 0 End) LastRacePoints,
Sum(Case When r.track_id <= '$chosentrack' -1 AND r.track_id >= l.start_race
Then total_points Else 0 End) TotalPoints
FROM members m
Join members_leagues l
On l.member_id = m.member_id
Join member_results r
On r.member_id = m.member_id
Where l.league_id = '$chosenleague'
Group By m.member_id
Order By TotalPoints Desc, LastRacePoints DESC, m.teamname Desc ")
or die ("Error - could not display league");
Just replace Join member_results r with Left Join member_results r and it should work. You should get NULLs for the sums if there are no results.
Related
I have a MySQL query in my php script, which is working fine currently. The only issue I have is that some of the columns return null values.
If there is data for those columns then they return the value, but if there is no data or records for the date then they return null. All I want to do is modify this query so that if anything is null it returns '0'.
I'm not sure if I should use IFNULL or coalesce but either way I'm unfamiliar with the best way to apply it to this query.
Any help is much appreciated.
$stmt3 = mysqli_prepare($conn2,
"UPDATE ambition.ambition_totals a
INNER JOIN
(SELECT
c.user AS UserID,
COUNT(*) AS dealers,
ROUND((al.NumberOfDealers / al.NumberOfDealerContacts) * 100 ,2) AS percent
FROM jfi_dealers.contact_events c
JOIN jackson_id.users u
ON c.user = u.id
JOIN jfi_dealers.dealers d
ON c.dealer_num = d.dealer_num
LEFT JOIN (
SELECT user_id, COUNT(*) AS NumberOfDealerContacts,
SUM(CASE WHEN ( d.next_call_date + INTERVAL 7 DAY) THEN 1 ELSE 0 END) AS NumberOfDealers
FROM jackson_id.attr_list AS al
JOIN jfi_dealers.dealers AS d ON d.csr = al.data
WHERE al.attr_id = 14
GROUP BY user_id) AS al
ON al.user_id = c.user
GROUP BY UserID) as cu
on cu.UserID = a.ext_id
SET a.dealers_contacted = cu.dealers,
a.percent_up_to_date = cu.percent;
") or die(mysqli_error($conn2));
UPDATE
Version with IFNULL statment:
UPDATE ambition.ambition_totals a
INNER JOIN
(SELECT
c.user AS UserID,
ifnull(count(*),0) AS dealers,
ifnull(ROUND((al.NumberOfDealers / al.NumberOfDealerContacts) * 100 ,2),0) AS percent
FROM jfi_dealers.contact_events c
JOIN jackson_id.users u
ON c.user = u.id
JOIN jfi_dealers.dealers d
ON c.dealer_num = d.dealer_num
LEFT JOIN (
SELECT user_id, COUNT(*) AS NumberOfDealerContacts,
SUM(CASE WHEN ( d.next_call_date + INTERVAL 7 DAY) THEN 1 ELSE 0 END) AS NumberOfDealers
FROM jackson_id.attr_list AS al
JOIN jfi_dealers.dealers AS d ON d.csr = al.data
WHERE al.attr_id = 14
GROUP BY user_id) AS al
ON al.user_id = c.user
WHERE c.created_at >= CURDATE()
GROUP BY UserID) as cu
on cu.UserID = a.ext_id
SET a.dealers_contacted = cu.dealers,
a.percent_up_to_date = cu.percent;
Yes, you can use IFNULL for this. But be very sure that you actually want this behaviour. PHP is also familiar with NULL values and can handle them just fine. 0 has a very different meaning. But if you do want this behaviour, simply wrap the field or statement that might return null in a IFNULL, for example:
SELECT IFNULL(user_id, 0);
But you can also do this in PHP itself, so you do not have to modify the query:
if (is_null($result['field'])) {
echo 0;
}
Or, if you use PHP 7+, you can also use the null coalescing operator:
echo $result['field'] ?? 0;
select c.kupon, count(*) as count
from kuponbahis c
join bahis b
on b.sonuc = c.secim
and b.ID = c.bahis
group by c.kupon
having count(case when c.bahis ='$sonuclandirilacakbahis' then 1 end) > 0
With this query I'm getting kupon IDs and counts. Then fetching them in PHP and matching with the result of
SELECT COUNT(*) FROM kuponbahis WHERE kupon='$kuponid'
(while fetching the second query, too.) If it's a match, i'm doing some work.
But now, I want to do this in SQL directly.
My PHP code is below;
$kazananKuponlariGetirSorgu = mysql_query("select c.kupon, count(*) as count
from kuponbahis c
join bahis b
on b.sonuc = c.secim
and b.ID = c.bahis
group by c.kupon
having count(case when c.bahis ='$sonuclandirilacakbahis' then 1 end) > 0");
while ($kazananKuponlariGetirSorgux = mysql_fetch_array($kazananKuponlariGetirSorgu)){
$kuponid = $kazananKuponlariGetirSorgux[0];
$tutanbahisadet = $kazananKuponlariGetirSorgux[1];
$kupondakiBahisAdeti = mysql_query("SELECT COUNT(*) FROM kuponbahis WHERE kupon='$kuponid'");
$kupondakiBahisAdetix = mysql_fetch_array($kupondakiBahisAdeti);
if ($kupondakiBahisAdetix[0]==$tutanbahisadet){
//it's a match
}
I tried many queries but they all failed. How can I merge this two sql process to only one?
The obvious way is in the having clause:
select c.kupon, count(*) as count
from kuponbahis c join
bahis b
on b.sonuc = c.secim and b.ID = c.bahis
group by c.kupon
having sum(c.bahis = '$sonuclandirilacakbahis') > 0 and
count(*) = (SELECT COUNT(*) FROM kuponbahis WHERE kupon = '$kuponid');
I been trying different combination, but I cant seems to get this to work. I have inner join tables, I want to count the number of QA ISSUE found in the records and also output those records with only QA ISSUE, How would I do that?
SELECT d.department, m.mo_number, m.part_number, c.category,
COUNT(CASE WHEN c.category = 'QA ISSUE' THEN category END) as qa_issue,
SUM(CASE WHEN c.category = 'QA ISSUE' THEN time_spent END) as time_spent
FROM master as m
INNER JOIN category as c ON c.cat_id = m.cat_id
INNER JOIN department as d ON d.dept_id = m.dept_id
WHERE m.date_created >= DATE_SUB(now(), INTERVAL 50 DAY) AND
d.department = 'Electronics'
GROUP BY m.mo_number
ORDER BY 1
To filter results by aggregates you use the HAVING clause which occurs after the GROUP BY clause. Note this is not a substitute for the WHERE clause (which chooses the rows to be aggregated).
SELECT
d.department
, m.mo_number
, m.part_number
, c.category
, COUNT(*) AS qa_issue
, SUM(time_spent) AS time_spent
FROM master AS m
INNER JOIN category AS c ON c.cat_id = m.cat_id
INNER JOIN department AS d ON d.dept_id = m.dept_id
WHERE m.date_created >= DATE_SUB(now(), INTERVAL 50 DAY)
AND d.department = 'Electronics'
AND c.category = 'QA ISSUE'
GROUP BY
d.department
, m.mo_number
, m.part_number
, c.category
HAVING COUNT(*) = 1
ORDER BY
d.department
I have also added a condition to the where clause and added all non-aggregated columns into the GROUP BY clause - which I recommend you do always.
I need to count record year wise, I did some query but i am not getting correct result. Below is my query. But that is not working for me. Can anyone please look in this and give me right query ? Any help will be appreciated.
SELECT
(SELECT count(DISTINCT id) FROM call_response WHERE disposition=0 AND user_id=pu.id ) AS `trueAlarm`,
(SELECT count(DISTINCT id) FROM call_response WHERE disposition=1 AND user_id=pu.id ) AS `falseAlarm`,
(SELECT count(DISTINCT id) FROM call_response WHERE disposition=2 AND user_id=pu.id ) AS `disregarded`,
YEAR(cr.created_date) AS `callYear`
FROM `call_response` AS `cr`
INNER JOIN `permit_users` AS `pu`
ON cr.user_id=pu.id
WHERE ( pu.is_deleted=0 AND pu.is_trashed=0 AND cr.is_deleted=0)
GROUP BY `callYear`
The query that you want uses either conditional aggregation or subqueries, but not both. In other words, either use the subqueries but do not have an outer join to call_response. Or, have the outer join but not the subqueries.
I would write the query like this:
SELECT count(distinct case when disposition = 0 AND user_id = pu.id then id end) as trueAlarm,
count(distinct case when disposition = 1 AND user_id = pu.id then id end) as falseAlarm,
count(distinct case when disposition = 2 AND user_id = pu.id then id end) as disregarded,
YEAR(cr.created_date) AS `callYear`
FROM `call_response` `cr` INNER JOIN
`permit_users` `pu`
ON cr.user_id = pu.id
WHERE pu.is_deleted = 0 AND pu.is_trashed = 0 AND cr.is_deleted = 0
GROUP BY `callYear`;
I had a mysql query where i need to add some condition in Group by statement , if i use single field in Group by it works but i need two field include in the Group by, here is my query any one please help me to find out the issue
SELECT (CASE
WHEN CSR.skill_type = 1 THEN
(SELECT skills_value from cv_skills
WHERE skills_id = CSR.skill_id )
WHEN CSR.skill_type = 2 THEN
(SELECT ostype_name from cv_os_type
WHERE ostype_id = CSR.skill_id )
WHEN CSR.skill_type = 3 THEN
(SELECT dbtype_name from cv_db_type
WHERE dbtype_id = CSR.skill_id)
WHEN CSR.skill_type = 4 THEN
(SELECT title from candidate_competencies
WHERE id = CSR.skill_id)
WHEN CSR.skill_type = 0 THEN
IT.type_name
END) AS skill_name,
(CASE
WHEN IT.type_parent_id > 0 THEN
IT.type_parent_id
WHEN IT.type_parent_id = 0 THEN
CIS.interview_type
END) AS typeId,
(CASE
WHEN CSR.skill_type = 4 THEN
minimum_rating
END) AS minimum_rating,
AVG( CSR.rate ) AS skill_rating,
CSR.skill_type,CIS.created, CC.id
FROM `candidate_interview_skill_rate` `CSR`
LEFT JOIN `candidate_interview_process` `CIP` ON CSR.interview_process_id = CIP.id
LEFT JOIN `candidate_interview_schedule` `CIS` ON CIS.id = CIP.interview_schedules_id AND CIS.archive_date IS NULL
LEFT JOIN `candidate_interview` `CI` ON CI.id = CIS.interview_id AND CI.archived_date IS NULL
LEFT JOIN `interview_type` `IT` ON IT.id = CIS.interview_type
LEFT JOIN `candidate_competencies` `CC` ON CC.id = CSR.skill_id
WHERE CI.candidate_user_id = 39
GROUP BY (CASE
WHEN CSR.skill_type > 0 THEN
CSR.skill_id, CSR.skill_type
ELSE CIS.interview_type
END)
ORDER BY `CSR`.`skill_type`
You can concatenate CSR.skill_id, CSR.skill_type to a single column and use it in select statement. The group by needs to be like this
GROUP BY CASE
WHEN CSR.skill_type > 0 THEN
CONCAT(CSR.skill_id, '-', CSR.skill_type)
ELSE
CIS.interview_type
END;
Hope this works.
You can include multiple expressions in a GROUP BY clause, just separate the expressions with commas, just like in the SELECT list.
Very often, the expressions in the SELECT list are repeated in the GROUP BY clause.
It's often possible to use a CASE expression to support a more complex set of conditions.
It's hard to provide more concrete assistance with your query, absent sample data, and desired output.