Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:35:13+00:00 2026-05-25T21:35:13+00:00

Hi I have a query that is giving me a few problems and it

  • 0

Hi I have a query that is giving me a few problems and it was suggested I ask a separate question about the end result rather than the problem.

So I have three tables and some user input.

the tables are:

  • users,
  • usersLanguages and
  • usersSkills

each table has a related ID the users table has ID and on the other two they have userID to match skills and languages to users

the user input is dynamic but for example it can be 1 for usersLanguages and 2 for usersSkills

The user input is taken from a form and what i need to do is match get the results of users
depending on the language IDs or skill ids passed through. for example i can pass two user ids and three language ID’s.

SELECT DISTINCT users.ID, users.name
FROM users 
INNER JOIN usersSkills 
ON users.ID = usersSkills.userID 
INNER JOIN usersLanguages ON users.ID = usersLanguages.userID 
WHERE activated = "1" 
      AND type = "GRADUATE" AND usersSkills.skillID IN(2) 
      AND usersLanguages.languageID IN(2)
GROUP BY usersSkills.userID HAVING COUNT(*) = 1,
         usersLanguages.userID HAVING COUNT(*) = 1 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:35:14+00:00Added an answer on May 25, 2026 at 9:35 pm

    You do not mix group by and having clauses.
    having is not a part of group by, in fact you can have having without a group by, in which case it will work as a more capable (and slower) where clause.

    SELECT u.ID, u.name
    FROM users u 
    INNER JOIN usersSkills us ON u.ID = us.userID 
    INNER JOIN al ON u.ID = ul.userID 
    WHERE u.activated = '1' 
          AND u.type LIKE 'GRADUATE' AND us.skillID IN('2') 
          AND ul.languageID IN('2')
    GROUP BY u.ID
    HAVING COUNT(*) = 1
    
    • because of the join criteria, ul.userID = us.userID = u.ID so it makes no sense to group by both. Just group by u.id, because that’s the ID you select after all.
    • u.name is functionally dependent on ID, so that does not need to be listed (*).
    • When doing a test like u.type = 'GRADUATE', I prefer to do u.type LIKE 'GRADUATE' because LIKE is case insensitive and = may not be depending on the collation, but that’s just me.
    • Distinct is not needed; group by already makes the results distinct.

    Having works on the resultset up to and including group by, so in this case you need only one having clause. If you want to have more, you need to treat them the same as a where clause:

    having count(*) = 1 AND SUM(money) > 10000 
    

    (*) this is only true in MySQL, in other SQL’s you need to list all non-aggregated selected columns in your group by clause. Whether they are functionally dependent on the group by column or not. This slows things down and makes for very long query-syntax, but it does prevent a few beginners errors.
    Personally I like the MySQL way of doing things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that is giving me problems and I can't understand why
I have a very simple query that's giving me unexpected results. Hints on where
I have a query that exhausts the default MAXRECURSION limit of 100. Giving me
This is giving me a headache. I have this link query here that grabs
I have a MySQL query that I'd like help optimizing. Its rather simple in
I have a T-SQL query that's giving me the most recent office visit in
I have this query, that's been giving me some issues, it looks like this:
I have an query that has been giving me trouble. I think there is
I am using in C# MYsql .I have query that works if I run
I have a query that is dynamically built after looking up a field list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.