Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Count MySQL value and ignore duplicate

Tags:

php

mysql

I'm having difficulties counting how many times it finds a new personnel_id in table planning but ignoring if it finds the same personnel_id a second time.

Exemple personnel_id 24 is on building A and building B next week but count only as 1 because this is the same person.

In the end I would do something like $result = $totalpersonnel_id_tablepersonnel - $totalpersonnel_id_tableplanningand it would give me a number like 7 meaning 7 employees has no building affected.

I think this is the correct query for what I want:

$test=mysql_query("SELECT COUNT(DISTINCT personnel_id) AS numberpid 
FROM planning WHERE personnel_id!='' GROUP BY personnel_id HAVING 
COUNT(personnel_id) > 1");

I don't find how to get the sum from this query.

like image 350
BackTrack57 Avatar asked Nov 09 '22 11:11

BackTrack57


1 Answers

if personnel_id!='' mean NOT NULL use personnel_id IS NOT NULL instead and having use numberpid > 1 so query after edited below

SELECT COUNT(DISTINCT personnel_id) AS numberpid 
FROM planning WHERE personnel_id IS NOT NULL 
GROUP BY personnel_id 
HAVING numberpid > 1
like image 188
Chanom First Avatar answered Nov 14 '22 22:11

Chanom First