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 7499715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:55:05+00:00 2026-05-29T19:55:05+00:00

I have this query that retreives a list of id’s + NAME: $sql =

  • 0

I have this query that retreives a list of id’s + NAME:

$sql = "SELECT id FROM #__table1 ";

$sql .= " WHERE (";
foreach($explode_tags as $k=>$explode_tag) {
  $sql .= "name = ".$db->Quote(trim($explode_tag));
  if(($k+1) != count($explode_tags))
  $sql .= " OR ";
}
$sql .= ")";        

$db->setQuery($sql);
$results = $db->loadResultArray();  

The result is an array like this:

keywordID    |    NAME
1                 cat
2                 dog
3                 horse

Now I have this table2:

id   |    ItemID    |    keywordID
1            4              1
2            4              2
3            4              3
4            6              1
5            6              2
6            7              1

I want to find from table2 all ItemID’s that have all keywordID’s found in table1.
In the example above I want to return only itemID 4 that has all keywords (all 3 of them).

I am running this query but I am not getting results:

...
$query .= " AND i.id IN (SELECT itemID FROM #__table2 WHERE (";
foreach($results as $k=>$result) {
$query .= "keywordID = ".(int)$result;
if(($k+1) != count($results))
$query .= " AND ";
}
$query .= "))";     
  • 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-29T19:55:06+00:00Added an answer on May 29, 2026 at 7:55 pm

    UPDATE

    Sorry, I miss read your question. I’ve done simple test using this data:

    id  itemId  keywordId
    1   4       1
    5   4       2
    6   4       3
    7   5       2
    8   5       3
    9   6       1
    10  6       2
    11  6       3
    12  7       3
    13  9       3
    14  9       2
    15  9       1
    

    and using this query:

    SELECT itemId, GROUP_CONCAT( keywordId ORDER BY keywordId ) AS crpcnct, COUNT( itemId ) 
    FROM  `temporary_table_123` 
    GROUP BY 1 
    HAVING crpcnct =  '1,2,3' 
    

    I can get the value that you wanted:

    itemId  crpcnct  count(itemId)
    4       1,2,3    3
    6       1,2,3    3
    9       1,2,3    3
    

    To achieve this, all you have to do is build the keywordID you want to use:

    $keywordIds[] = $results['keywordId'];
    

    and then sort accending

    sort($keywordIds);
    

    the last step is, supply this array into query:

    SELECT itemId, GROUP_CONCAT( keywordId ORDER BY keywordId ) AS crpcnct, COUNT( itemId ) 
    FROM  `temporary_table_123` 
    GROUP BY 1 
    HAVING crpcnct =  '" . implode(",", $keywordIds) . "'
    

    There you have it.

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

Sidebar

Related Questions

So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have an NHibernate query that looks like this: var query = Session.CreateQuery(@ select
I have a Linq query that looks something like this: var query = from
I have a query that retrieves a list of received part shipments from plants.
So I have this query: SELECT DISTINCT(b.friend_id) AS possible_id FROM friend_friends a JOIN friends_friends
I have this linq query that works well (although it may be written better,
This query that I have is returning therapists whose 'therapistTable.activated' is equal to false
I have this query to fetch the total OrderStatus that have values 1 and
I have this query statement and want to only get records that has a

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.