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

  • Home
  • SEARCH
  • 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 4568900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:08:32+00:00 2026-05-21T19:08:32+00:00

I was wondering if anybody here could help with a problem I am having.

  • 0

I was wondering if anybody here could help with a problem I am having. I am trying to combine 2 mysql queries into one query using sub queries. I currently have the 2 queries separatly which produce the results i want:

Here are the 2 queries:

SELECT contact_id FROM contacts 
WHERE acc_id = 1 AND email LIKE "paul%"

SELECT c0.contact_id 
FROM contact_tags c0 INNER JOIN contact_tags c1 
on c0.contact_id = c1.contact_id INNER JOIN contact_tags c2 
on c1.contact_id = c2.contact_id where c0.tag_id = 1 
AND c1.tag_id = 2 AND c2.tag_id = 3

Here is some example data from the tables:

Contacts:

contact_id    acc_id     email
    54          1        paul@test.com

Tags:

id     contact_id     tag_id 
1         54            1
2         54            2
3         54            3
4         50            1
5         50            2

Both queries when run independently produce this result which is correct:

contact_id
    54

However I am trying to nest one query inside the other to produce the same result from a single query:

Here is what I have tried:

SELECT c0.contact_id 
FROM 
(
  SELECT contact_id 
   FROM contacts 
    WHERE acc_id = 1 AND email LIKE "paul%"
) AS c0 
LEFT JOIN contact_tags AS c1 
ON c1.contact_id = c0.contact_id 
AND (
  SELECT c0.contact_id FROM contact_tags c0 
  INNER JOIN contact_tags c1 
  on c0.contact_id = c1.contact_id 
  INNER JOIN contact_tags c2 on c1.contact_id = c2.contact_id 
  where c0.tag_id = 1 AND c1.tag_id = 2 AND c2.tag_id = 3
   ) 
 WHERE c1.id IS NOT NULL

However I know this is not right as i want to return just the single unique contact id which matches all conditions as above:

contact_id
   54
   54
   54
   54

If anyone could help me out with this it would be much appreciated.

Thanks

  • 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-21T19:08:33+00:00Added an answer on May 21, 2026 at 7:08 pm
    Select contact_id
    From contacts
    Where acc_id = 1 And email Like 'paul%'
        And contact_id In   (
                                Select Tags1.contact_id
                                From contact_tags As Tags1
                                Where Tags1.tag_id In(1,2,3)
                                Group By Tags1.contact_id
                                Having Count( Distinct Tags1.tag_id ) = 3
                                )
    

    Or:

    Select contact_id
    From contacts
    Where contact_id In (
                            Select C1.contact_id
                            From contact_tags As Tags1
                                Join contacts As C1
                                    On C1.contact_id = Tags1.contact_id
                            Where Tags1.tag_id In(1,2,3)
                                And C1.acc_id = 1
                                And C1.email Like 'paul%'
                            Group By C1.contact_id
                            Having Count( Distinct Tags1.tag_id ) = 3
                            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.