SQL Query Help

Posted on 2011-05-03
Last Modified: 2012-06-21
I'm trying to build a SQL query for my PHP code that will count & display the number of assignments from data within my MySQL database. I only need to display & count  the active and the ones locaed in office2. Below is my SQL query but it is displaying & counting all workers:

$query="SELECT worker, active, COUNT(worker) FROM psrinfo WHERE location = 'office2' AND active = '1'  ";

I have a total of 10 workers. I only need to display worker1, worker2, worker3, worker4, worker6 and worker8.

At this time it counts all workers 1 - 10
Question by:wantabe2
    LVL 6

    Expert Comment

    take the COUNT(worker) out and paste your results.

    LVL 15

    Author Comment

    okay, if I can get the below to work, it will fix my issue.

    Instead of writing a fancy SQL can I just pick & choose what I need the SQL to pull out & count? If I remove AND worker = 'worker6' it displays & counts the number of assignments for worker1 but when I add it it displays nothing.... At this time I only need it to count the number of times it see's worker1 & worker 6 in the worker field & ignore the others...
    $query="SELECT worker, active, COUNT(worker) FROM psrinfo WHERE location = 'office2' AND active = '1' AND worker = 'worker1' AND worker = 'worker6' ";

    Open in new window

    LVL 6

    Accepted Solution

     select count(*)
        from psrinfo
     where location = 'office2'
        AND active = '1'
        AND worker IN ('worker1','worker6')


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now