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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:51:11+00:00 2026-05-27T01:51:11+00:00

I have two tables that make up a full text index of article content

  • 0

I have two tables that make up a full text index of article content for search purposes. One of the tables is just a primary key associated with a word, whereas the other records the article it occurred in and its location in the document. A single word can conceivably appear many times in the same document with different locations, so the same word id can occur several times in the word_locations table.

Here are the structures:

words:

id          bigint
word        tinytext

word_location:

id          bigint(20)  
wordid      bigint(20)  
location    int(11) 
article_id  int(11)

What i need to write is a query that will find the count of occurrences for each word for any one profile. I need to preserve a zero value for wordids that don’t appear at all, so I assume this needs to be a left join. However, whenever I try to add a where query to limit off article, any wordids that don’t appear at all are not included in the result set.

I have tried:

select words.wordid, COUNT(word_location.wordid) as appears from words left join word_location on word.id = word_location.wordid where article_id = %s GROUP BY wordid

But this query does not return zeros for words that don’t appear at all.

How can I modify this left join?

Thanks in advance!

EDIT:

Here is an example data set and the result sets for the different queries.

Example article content:

Bob’s Restaurant is one of the finest restaurants in greater
County where you can enjoy the finest Turkish Cuisine.

So the vocabulary table, after being adjusted by the application to exclude stop words, will have in its vocabulary rows for Bob, Restaurant, finest, greater, county, enjoy, Turkish, and cusine. (I’m using this actual article since it’s the first in the set, so the ids actually appear starting from integer 1.

The query provided by @Mark Bannister produces this result set:
wordid – word – occurances:

128 clifton 0
1   bob's   2
2   restaurant  2
3   one 1
4   finest  3
5   restaurants 2
6   greater 1
9   county  1
12  enjoy   3
13  turkish 6
14  cuisine 1

The result set is correct per se – but id 128 doesn’t appear in the document at all and is the only thing in the result set with occurance 0. The goal is to have the entire vocabulary returned with number of occurrences from the document (this is roughly 2500 different words)

My original problematic query from before the edit above actually returned the same result set, but without ANY 0 occurance rows at all.

  • 1 1 Answer
  • 2 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-27T01:51:12+00:00Added an answer on May 27, 2026 at 1:51 am

    You need to include your article selection in your join condition:

    select words.wordid, COUNT(word_location.wordid) as appears 
    from words 
    left join word_location on word.id = word_location.wordid and article_id = ? 
    GROUP BY wordid
    

    Including the restriction on article_id in the WHERE clause effectively turns your left join back into an inner join.

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

Sidebar

Related Questions

I have two tables that link together through an id one is submit_moderate and
I have two tables that have virtually identical content and very similar structure. They
Suppose I have two tables that are linked (one has a foreign key to
I have two tables that I need to compare and update values in one
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have two tables that should be joined together by a foreign key relationship,
I have two tables that are considered a single entity in my domain model.
I have two tables that I am joining with the following query... select *
I have two tables that are related via a mapping table: keywords titles I
Let's say I have two tables that look like this: TH TH TH TH

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.