+ Reply to Thread
Results 1 to 3 of 3

Thread: SQL Sub Query

  1. #1
    Barn Newbie smti is an unknown quantity at this point smti's Avatar
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    3

    SQL Sub Query

    Hi,

    I have a database which contains student names (among other things). The students can interface with the database to submit service requests. Now for the problem: Students are submitting multiple requests. We need to be able to easily locate these students. Essentially we need to accomplish the following:

    1. Query the database and display all student's names currently in the DB.

    2. Count the number of records which contain the same student name.

    I have written the following code in an attempt to handle the task, but it does not seem quite right. Here is the code:

    PHP Code:
    //Step 1: Grab all names from the database!

    include("connection.inc.php");


     
    $sql "SELECT ticketid, name FROM tickets limit 30";
     
    $result pg_query($dbh$sql);
     if (!
    $result) {
         die(
    "Error in SQL query: " pg_last_error());
     }
      
     
    // iterate over result set
     // print each row
        // keeps getting the next row until there are no more to get
           
    while ($row pg_fetch_array($result)) {
            
    $ticketid $row['ticketid'];
            
    $student_name $row['name'];
            
            
    //Now display the output
                
    echo $student_name;
                echo 
    "<br />";


    include (
    "connection.inc.php");
       
    $numtickets pg_query($dbh"select * from tickets where name='$student_name'");
       
    $numtickets pg_num_rows($numtickets);    
       
    //END THE WHILE STATEMENT
    }
    echo 
    "<br />";
    echo 
    "Number of students with multiple entries: " .$numtickets

    Any help would be greatly appreciated!

    Thanks,

    smti

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    I think you could achieve that in one query.Something like:
    Code:
    SELECT name,COUNT(ticketid) AS numberTickets FROM tickets GROUP BY name
    
    One of our SQL gurus would be able to confirm that though...

  3. #3
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    To find students with multiple records:
    Code:
    Select name, count(ticketID) as numTickets
    From tickets
    Group By Name
    Having count(ticketID) > 1
    
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. SQL Query in Excel csv using MS Query
    By richyrich in forum Microsoft Office
    Replies: 2
    Last Post: August 11th, 2009, 11:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO