Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-25-2011, 12:10 PM   PM User | #1
techker
New Coder

 
Join Date: Sep 2005
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
techker is an unknown quantity at this point
stuck in query..

hey guys this is the structure of the Db:

`Ex_ID` int(11) NOT NULL auto_increment,
`N_Etudiant` varchar(25) NOT NULL,
`Professeur` varchar(50) NOT NULL,
`Foyer` varchar(50) NOT NULL,
`Motif` varchar(50) NOT NULL,
`Date` date NOT NULL,
`Notes` text NOT NULL,
`Autre` varchar(150) NOT NULL,

so basically N_Etudiant is a student name..

so when we insert it looks like

`N_Etudiant` Mike
`Professeur` mrpopins
`Foyer` 301
`Motif` fight
`Date` 2011/01/01
`Notes` none
`Autre` 0

so the query i need is to flag if (N_Etudiant) has the same (Professeur) more the 2 times

cause we need to know if the student (N_Etudiant) has been expelled from class by the same teacher (Professeur) more then 2 times..

is this clear?lol
techker is offline   Reply With Quote
Old 11-25-2011, 12:26 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
What have you tried?
guelphdad is offline   Reply With Quote
Old 11-25-2011, 12:31 PM   PM User | #3
techker
New Coder

 
Join Date: Sep 2005
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
techker is an unknown quantity at this point
i have tried

SELECT * FROM `Expulsion` WHERE `N_Etudiant`=`Professeur` >= 2

WHERE N_Etudiant= 2 AND Professeur >= 2

and more..but i forget..
techker is offline   Reply With Quote
Old 11-25-2011, 12:38 PM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Do you know how GROUP BY works? It collapses rows into groups, so if you group by N_Etudiant it would give you all the rows for each student into one. you could then count that group.

That would give you how many times a student appeared in the table though and you want it how many times per teacher, so you'd group on N_Etudiant, Professeur.

that would give you how many times a student shows with each professor.

Does that make sense to you?

Now use a HAVING clause to count each group for what you are looking for.
guelphdad is offline   Reply With Quote
Old 11-25-2011, 12:51 PM   PM User | #5
techker
New Coder

 
Join Date: Sep 2005
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
techker is an unknown quantity at this point
ok so i started with

SELECT *
FROM `Expulsion`
GROUP BY `Professeur`
LIMIT 0 , 30

the added

SELECT *
FROM `Expulsion`
GROUP BY `Professeur`
HAVING COUNT( `Professeur` ) =2
LIMIT 0 , 30

the i think i got it

SELECT * FROM `Expulsion` GROUP BY `Professeur` HAVING COUNT( `Professeur` ) >2

i feel im close..but the results are not good..

Last edited by techker; 11-25-2011 at 12:55 PM..
techker is offline   Reply With Quote
Old 11-25-2011, 01:07 PM   PM User | #6
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
First, get out of the habit entirely of using SELECT * except in very specific cases.

Your results aren't good because you collapsed your rows by professor only and not by each student as well.

First just figure out the student, professor, you are close:

Code:
SELECT
  N_Etudient,
  Professeur
FROM
  Expulsion
GROUP BY
  N_Etudient,
  Professeur
HAVING 
  COUNT(*) >= 2
without the group by student you were only counting how many times a professor showed up so if he showed up twice for Gary and once for Tim he would show up a total of three times. and you only want it for Gary.

Last edited by guelphdad; 11-25-2011 at 01:24 PM..
guelphdad is offline   Reply With Quote
Users who have thanked guelphdad for this post:
techker (11-26-2011)
Old 11-25-2011, 11:25 PM   PM User | #7
techker
New Coder

 
Join Date: Sep 2005
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
techker is an unknown quantity at this point
nice its working...
i want to email the results..

can i put an if COUNT(*) >= 2

if (COUNT(*) >= 2){;
email
else
do nothing

};

ok so i got this going.email part works but i can't seem to echo the teacher and student name?

PHP Code:
$query "SELECT N_Etudiant, Professeur
FROM Expulsion
GROUP BY N_Etudiant, Professeur
HAVING COUNT( * ) >=2
LIMIT 0 , 30"

$result mysql_query ($query) or die ('Your query did not match any results: ' mysql_error()); 







                    if (
mysql_num_rows($result)>0)  
                 
                        {                     

                            while(
$row=mysql_fetch_array($result)) 
                         
                                { 
                                
                                
// multiple recipients
$to  'techker@gmail.com'// note the comma


// subject
$subject 'Suivis Requis';

// message
$message '
<html>
<head>
  <title>Un suivis est requis</title>
</head>
<body>
  <p>Etudiant</p>
  <table>
    <tr>
      <th>Enseignant(e)$row[\"Professeur\"]</th>
    </tr>
    
  </table>
</body>
</html>
'
;

// To send HTML mail, the Content-type header must be set
$headers  'MIME-Version: 1.0' "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";


// Mail it
mail($to$subject$message$headers);

                                }
                                } 

Last edited by techker; 11-25-2011 at 11:48 PM..
techker is offline   Reply With Quote
Old 11-26-2011, 02:26 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
PHP will *NOT* do replacement of PHP variables inside of strings delimited by apostrophes.

That is,
Code:
$x = 'example';
$msg = 'this is an $x but it does not work';
That *only* works inside strings delimited by quotes.

So
Code:
$x = 'example';
$msg = "this is an $x that does works!";
So you can't use $row[\"Professor\"] as you are doing.

You could do:
Code:
$prof = $row["Professor"];
$message = "....<th>Enseignant(e) $prof</th>...";
Or you could do
Code:
$message = '....<th>Enseignant(e) ' . $row["Professeur"] . '</th> .... ';
You need to learn what PHP is capable of and what it can't do.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
techker (11-26-2011)
Old 11-26-2011, 02:28 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
can i put an if COUNT(*) >= 2

if (COUNT(*) >= 2){;
email
else
do nothing

};
You don't *NEED* to. The query will *ONLY* return results where the COUNT IS ALREADY >= 2.

You will NEVER SEE any records where COUNT is 1 or 0.

So if you you find it in the results, you need to send an email.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
techker (11-26-2011)
Old 11-26-2011, 08:37 PM   PM User | #10
techker
New Coder

 
Join Date: Sep 2005
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
techker is an unknown quantity at this point
ok cool.i get it.
Old Pedant thx!

i will try it out tonight!keep you posed
techker is offline   Reply With Quote
Old 11-26-2011, 08:50 PM   PM User | #11
techker
New Coder

 
Join Date: Sep 2005
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
techker is an unknown quantity at this point
Alright so it sums up to this..really appreciat the help guys!super forum!!

PHP Code:

$query 
"SELECT N_Etudiant, Professeur
FROM Expulsion
GROUP BY N_Etudiant, Professeur
HAVING COUNT( * ) >=2
LIMIT 0 , 30"

$result mysql_query ($query) or die ('Your query did not match any results: ' mysql_error());                     
$row mysql_fetch_assoc($result);
                            
$prof $row["Professeur"];
$eleve $row["N_Etudiant"];
                                
// multiple recipients
$to  'techker@gmail.com'// note the comma


// subject
$subject 'Suivis Requis';

// message
$message "Suivis avec $prof pour Etudiant $eleve...";

// To send HTML mail, the Content-type header must be set
$headers  'MIME-Version: 1.0' "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";



// Mail it
mail($to$subject$message$headers); 
techker is offline   Reply With Quote
Old 11-26-2011, 08:52 PM   PM User | #12
techker
New Coder

 
Join Date: Sep 2005
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
techker is an unknown quantity at this point
by the way is there a way to remove the email?lol

techker@fugit.dnsbox16.com

this sis the email it sends the email with to..

how can i put my domain?
techker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:00 AM.


Advertisement
Log in to turn off these ads.