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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:36:52+00:00 2026-06-02T21:36:52+00:00

I’m trying to write a SQL statement that will allow me to select a

  • 0

I’m trying to write a SQL statement that will allow me to select a series of articles from a table based on their keywords. What I’ve got so far is a token table, an article table, and a many-to-many table for tokens & articles:

tokens
  rowid
  token

token_article
  token_rowid
  article_rowid

articles
  rowid

What I’m doing is taking a search query, splitting it up by spaces, then select all articles that contains those keywords. So far I’ve come up with this:

select * from 
    (select * from tokens 
        inner join token_article on 
             tokens.rowid = token_article.token_rowid and 
             token = 'ABC'
    ) as t1, 

    (select * from tokens 
        inner join token_article on 
            tokens.rowid = token_article.token_rowid and 
            token = 'DEF'
    ) as t2 

where t1.article_rowid = t2.article_rowid and t2.article_rowid = articles.rowid

Which works but of course its doing a select on all articles that match ABC and all articles that DEF then selecting them.

Now I’m trying to figure out a better way. What I imagine in my mind that would work would be to select all the articles that match ABC and from those match any with DEF. This is what I imagine it to look like but does not work (receive error message “no such columns: tokens.rowid”)

select * from 
    (select * from
        (select * from tokens 
                inner join token_article on 
                    tokens.rowid = token_article.token_rowid and
            token = 'ABC'
        ) 
    inner join token_article on 
            tokens.rowid = token_article.token_rowid and
    token = 'DEF'
)
  • 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-06-02T21:36:53+00:00Added an answer on June 2, 2026 at 9:36 pm

    Because there is more than one way to do this…this method uses GROUP BY and HAVING clauses. The query is looking for all articles that have either the ABC or DEF token, but then grouping by the article ID where the count of tokens for the article is equal to the number of tokens being queried.

    Note that I’ve used MSSQL syntax here, but the concept should work in most SQL implementations.

    Edit: I should point out that this has a fairly clean syntax as you add more tokens to the query. If you add more tokens, then you just need to modify the t.token_in criteria and adjust the HAVING COUNT(*) = x clause accordingly.

    DECLARE @tokens TABLE
    (
        rowid INT NOT NULL,
        token VARCHAR(255) NOT NULL
    )
    
    DECLARE @articles TABLE
    (
        rowid INT NOT NULL,
        title VARCHAR(255) NOT NULL
    )
    
    DECLARE @token_article TABLE
    (
        token_rowid INT NOT NULL,
        article_rowid INT NOT NULL
    )
    
    INSERT INTO @tokens VALUES (1, 'ABC'), (2, 'DEF')
    INSERT INTO @articles VALUES (1, 'This is article 1.'), (2, 'This is article 2.'), (3, 'This is article 3.'), (4, 'This is article 4.'), (5, 'This is article 5.'), (6, 'This is article 6.')
    INSERT INTO @token_article VALUES (1, 1), (2, 1), (1, 2), (2, 3), (1, 4), (2, 4), (1, 5), (1, 6)
    
    -- Get the article IDs that have all of the tokens
    -- Use this if you just want the IDs
    SELECT a.rowid FROM @articles a
    INNER JOIN @token_article ta ON a.rowid = ta.article_rowid
    INNER JOIN @tokens t ON ta.token_rowid = t.rowid
    WHERE t.token IN ('ABC', 'DEF')
    GROUP BY a.rowid
    HAVING COUNT(*) = 2 -- This should match the number of tokens
    
    rowid
    -----------
    1
    4
    
    -- Get the articles themselves
    -- Use this if you want the articles
    SELECT * FROM @articles WHERE rowid IN (
        SELECT a.rowid FROM @articles a
        INNER JOIN @token_article ta ON a.rowid = ta.article_rowid
        INNER JOIN @tokens t ON ta.token_rowid = t.rowid
        WHERE t.token IN ('ABC', 'DEF')
        GROUP BY a.rowid
        HAVING COUNT(*) = 2 -- This should match the number of tokens
    )
    
    rowid       title
    ----------- ------------------
    1           This is article 1.
    4           This is article 4.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.