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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:31:02+00:00 2026-05-27T06:31:02+00:00

I have 3 tables that look like this: Table 1 (main table): item_id (item_id,

  • 0

I have 3 tables that look like this:

  • Table 1 (main table):

    item_id (item_id, item_name) // item_id set to primary 
    row1    (1,       "item 1")
    row2    (2,       "item 2")
    row3    (3,       "item 3")
    
  • Table 2 (linking table):

    item_keywords (ikid, #item_id, #keyword_id) // ikid is set to primary 
    row1          (1,       1,     1)
    row2          (2,       1,     2)
    row3          (3,       1,     3)
    row4          (4,       2,     1)
    row5          (5,       3,     1) 
    
  • Table 3:

    keywords (keyword_id, keyword_name) // keyword_id is set to primary 
    row1          (1,       "key1")
    row2          (2,       "key2")
    row3          (3,       "key3")
    row4          (4,       "key4")
    row5          (5,       "key5") 
    

I would like to make a query that returns all items that contain certain keywords AND also don’t contain some other keywords.

If you look at the table data above you can see that:

Item 1 contains keywords with ids: 1,2 and 3; 
Item 2 contains keyword with id: 1 
item 3 contains keyword_id =1 

So I need a query that will return all items that contain key1 (keyword_id=1) and DON’T contain key3 (keyword_id=3). In this exmaple the query should return key2 (keyword_id=2), key3 (keyword_id=3).

Something like this:

SELECT a.*
FROM items a
JOIN item_keywords b ON b.item_id = a.item_id
WHERE b.keyword_id = 1
   AND b.keyword_id != 3

But this doesn’t work properly because the query returns key1,key2 and key3. The answer should be: key2 and key3. What would be the simplest way of doing this?

  • 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-27T06:31:03+00:00Added an answer on May 27, 2026 at 6:31 am
    SELECT i.*
    FROM items AS i
    INNER JOIN item_keywords AS ik ON i.item_id = ik.item_id
    WHERE keyword_id = 1
       AND ik.item_id NOT IN
       (
          SELECT item_id
          FROM item_keywords
          WHERE keyword_id = 3
       )
    

    Example of query working:

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

Sidebar

Related Questions

Let's say I have two tables that look like this: TH TH TH TH
I have a 3 tables that look like this: (source: InsomniacGeek.com ) On the
I have a table posts that could look like this: id | title |
On my pages I have a table with rows that typically look like this:
I have a number of mappers that look like this: mapper(Photo,photo_table, properties = {
I have two tables in my database that look like that: Customer: C_ID city
I have a table with contents that look similar to this: id | title
I have this table, I bet that looking at the tables, you will know
In my database I have tables that define types for example Table: Publication Types
I recently used display:table and display:table-cell properties. It was like this, <div id=main style=display:table>

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.