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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:39:58+00:00 2026-06-12T01:39:58+00:00

So, I have a database with a table called artcles, and also a table

  • 0

So, I have a database with a table called artcles, and also a table called article tags. When a user views an article, I want to query up to five articles that have tags similar to the one that is being viewed. Here are my two tables:

CREATE TABLE `articles` (
  `article_id` int(15) NOT NULL AUTO_INCREMENT,
  `parent_id` int(15) NOT NULL,
  `author_id` int(15) NOT NULL,
  `title` text NOT NULL,
  `content` text NOT NULL,
  `date_posted` text NOT NULL,
  `views` int(15) NOT NULL,
  `preview` text NOT NULL,
  `status` tinyint(1) NOT NULL,
  `modified_date` text NOT NULL,
  PRIMARY KEY (`article_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE `article_tags` (
  `tag_id` int(15) NOT NULL AUTO_INCREMENT,
  `article_id` int(15) NOT NULL,
  `keyword` varchar(250) NOT NULL,
  PRIMARY KEY (`tag_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

I’ve tried writing my own queries, but they never seem to work. I would like to use joins in the query instead of resorting to using CSV’s and LIKE. Here’s the query that I have so far:

SELECT A2.article_id, count(A2.article_id) AS matches
FROM article_tags AS A1 JOIN article_tags ON (A1.keyword = A2.keyword AND 1.article_id != A2.article_id)
JOIN articles ON (A2.article_id = A.article_id) AS A
WHERE A1.article_id = 1
GROUP BY A2.article_id
ORDER BY matches DESC
LIMIT 5"

This is my updated query:

$query = "
            SELECT t2.article_id, count(t2.keyword) AS matches
            FROM article_tags t1
            JOIN article_tags t2 ON (t1.keyword = t2.keyword AND t1.article_id != t2.article_id)
            WHERE t1.article_id = ".$article_id."
            GROUP BY t2.article_id
            ORDER BY matches DESC
            LIMIT 5";

This is the result of dumping the array with var_dump

array
  0 => 
    array
      'article_id' => string '2' (length=1)
      'matches' => string '1' (length=1)

$query = "
            SELECT t2.article_id, count(t2.keyword) AS matches
            FROM article_tags t1
            JOIN article_tags t2 ON (t1.keyword = t2.keyword AND t1.article_id != t2.article_id)
            WHERE t1.article_id = ".$article_id."
            GROUP BY t2.article_id
            ORDER BY matches DESC
            LIMIT 5";

        if($query = $this->db->query($query)){

            if($query->num_rows() > 0){

                foreach($query->result_array() as $id => $article){

                    $articles[$id] = $this->fetch_article($article['article_id']);

                }

            } else {

                $articles = array();

            }

        } else {

            $articles = array();

        }

        return $articles;

    }

  • 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-12T01:39:59+00:00Added an answer on June 12, 2026 at 1:39 am

    Basically your thinking is correct – make a self JOIN on article_tags table. There are something that you should improve:

    • COUNT tag_id instead of article_id, since you want to sort articles by relevance, and the count of matched tags indicates the relevance.
    • Join on tag_id instead of keyword. Join on non-indexed column will be a performance issue.
    • Do not use a != in JOIN condition for the reason of performance. Just get all related articles and simply remove the most related one, which should be the current article itself
    • Join on articles is not necessary, for the reason of performance. You don’t need the articles themselves; just do a simple SELECT on articles after you get ids of the 5 related articles.

    So the answer could be something like this:

    SELECT
        A2.article_id, count(A2.tag_id) AS matches
    FROM 
        article_tags A1 
    JOIN
        article_tags ON A1.tag_id=A2.tag_id
    WHERE
        A1.article_id = 1
    GROUP BY
        A2.article_id
    ORDER BY
        matches DESC
    LIMIT 6   -- instead of 5, because the first result would be the current article
    

    You should get an array with 6 ids, and just remove the first one, then do a SELECT(e.g. in python):

    article_ids = article_ids[1:]
    articles = cursor.execute(
        "SELECT * FROM articles WHERE article_id IN (%s)" % ",".join(article_ids)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a database table with a column called 'symbol', that is unique via
I have an database table called MenuItem (MenuItemID, ParentMenuItemID, MenuItemName). I want to use
I currently have a database with two tables called Articles and Tags . In
I have a database table called users This table has two columns (that are
I have a MYSQL database table called 'wp_quotes' with a field that contains a
So, I have a database (With a table called users) that let's users connect
I have a database table (called Info) that stores [Data], [fkID], [UserID], [Timestamp]. [Data]
I have a table called articles on the database articles : id +++ writer_id
I have a database table called posts that has 'id, title, slug and content'

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.