Closed Thread
Results 1 to 3 of 3

Thread: need help with a complex mysql join query

  1. #1
    mjfroggy is offline Newbie
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    need help with a complex mysql join query

    Hello,

    I need help with a complex mysql query.
    What I need to do is connect I have a table with staff and there supervisors. I also have another table that lists if the staff member is still active or not.

    So with my query I need to connect to the one table and pull the latest entry for all staff who are connexted to a specific supervisor I then need to connect to the other table to see if they are active or not. With the end result from the mysql query being a list of all active staff that are curretnly assigned to a specific supervisor.

    My issue is I am not sure how to do the join to the table that lists if the person is active or not. Here is my query code
    Code:
    $month '9';
    $year '2009';
    $groupwiseid 'jdoe';

    SELECT teams.rep
         
    teams.assign_dateEmployees.Employeestatusid  
      FROM 
    SELECT rep
                  
    MAX(assign_date) AS assignment
               FROM teams
    Employees
              WHERE MONTH
    (assign_date) <= $month AND YEAR(assign_date) = $year 
             GROUP
                 BY rep 
    ) AS latest
    INNER
      JOIN teams
        ON teams
    .rep         latest.rep
       
    AND teams.assign_date latest.assignmentEmployees ON Employees.GroupwiseID
     WHERE sup_manid 
    '$groupwiseid' 

    When I run the above in sql I get the following error
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON Employees.GroupwiseID WHERE sup_manid = 'imoss' LIMIT 0, 30' at line 12

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: need help with a complex mysql join query

    It would be better if you gave the table structures (schema).
    I'll have to make some assumptions, but your query should
    probably look something like this:

    Code:
    SELECT teams.rep,
           max(teams.assign_date) AS assign_date,
           Employees.Employeestatusid
    FROM teams INNER JOIN Employees
    ON teams.rep = Employees.rep
    GROUP BY teams.rep, Employees.Employeestatusid
    It's hard for me to understand what your exact criteria is,
    but it would be placed in a WHERE clause located after
    the ON line and before the GROUP BY line.

    Need more info for a more detailed solution.
    Good Luck

  4. #3
    cdg10620's Avatar
    cdg10620 is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Texas
    Posts
    387
    Blog Entries
    3
    Rep Power
    12

    Re: need help with a complex mysql join query

    Whenever you do a join it needs to say JOIN table1.column = table2.column. I'm assuming you have a syntax error due to the fact that you are saying JOIN ON Employees.GroupwiseID but you do not have "Employees.GroupwiseID" equal to another value. I can't tell much else without a table schema like debtboy stated above. If you have any other questions please let me know.
    -CDG10620
    Software Developer

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help on a MySql Query
    By webcodez in forum Database & Database Programming
    Replies: 4
    Last Post: 03-06-2011, 05:33 AM
  2. Improving my join query (MYsql)
    By bleastan in forum Database & Database Programming
    Replies: 11
    Last Post: 11-15-2010, 11:26 AM
  3. MySQL - Join and Where Help
    By BlaineSch in forum Database & Database Programming
    Replies: 4
    Last Post: 07-23-2010, 02:57 AM
  4. error in MySQL query
    By Orjan in forum Database & Database Programming
    Replies: 2
    Last Post: 05-22-2008, 05:21 PM
  5. MySQL Inner Join Query...
    By Lop in forum Database & Database Programming
    Replies: 3
    Last Post: 01-11-2007, 01:03 PM

Tags for this Thread

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