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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:26:21+00:00 2026-05-29T09:26:21+00:00

I have 5 tables: library_item ============ id title description index_text library_item_rel_category ========================= item_id category_id

  • 0

I have 5 tables:

library_item
============
id
title
description
index_text

library_item_rel_category
=========================
item_id
category_id

library_category
================
id
parent_id
name

library_item_rel_tag
====================
item_id
tag_id

library_tag
===========
id
name

And currently I have this MySQL request (Using PHP PDO):

SELECT
    i.*,
    ((
        ((MATCH (i.title) AGAINST (:terms)) * 5) +
        ((MATCH (i.description) AGAINST (:terms)) * 4) +
        ((MATCH (i.index_text) AGAINST (:terms)) * 3) +
        (MATCH (i.title, i.description, i.index_text) AGAINST (:terms))
    ) + IFNULL(c.score, 0) + IFNULL(t.score, 0)) as score
FROM 
    library_item AS i
LEFT JOIN
    (
        SELECT
            rel_c.item_id,
            ((MATCH(c.name) AGAINST (:terms)) * 5) AS score
        FROM 
            library_item_rel_category rel_c
        INNER JOIN
            library_category c ON rel_c.category_id = c.id
        WHERE
            MATCH(c.name) AGAINST (:terms)
        ORDER BY
            score DESC
    ) AS c ON c.item_id = i.id
LEFT JOIN
    (
        SELECT
            rel_t.item_id,
            ((MATCH(t.name) AGAINST (:terms)) * 5) AS score
        FROM 
            library_item_rel_tag rel_t
        INNER JOIN
            library_tag t ON rel_t.tag_id = t.id
        WHERE
            MATCH(t.name) AGAINST (:terms)
        ORDER BY
            score DESC
        LIMIT 1
    ) AS t ON t.item_id = i.id
WHERE
    i.is_archive = 0 AND
    ((
        ((MATCH (i.title) AGAINST (:terms)) * 5) +
        ((MATCH (i.description) AGAINST (:terms)) * 4) +
        ((MATCH (i.index_text) AGAINST (:terms)) * 3) +
        (MATCH (i.title, i.description, i.index_text) AGAINST (:terms))
    ) + IFNULL(c.score, 0) + IFNULL(t.score, 0)) > 5
GROUP BY 
    i.id
ORDER BY
    score DESC

I would like to add the ability to match the parent categories too until it hits the root. Is it possible using MySQL in this single query?

I am ready to change table structure if needed, it’s my first recursive tree.

  • 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-29T09:26:22+00:00Added an answer on May 29, 2026 at 9:26 am

    MySQL does not support recursive queries as some other databases do. There is no way to find all parent categories in a single query with the way you’re storing the parent_id.

    See my presentation Models for Hierarchical Data with SQL and PHP for an overview of different techniques for storing and querying tree-like structures in MySQL.

    See also my answer to What is the most efficient/elegant way to parse a flat table into a tree?

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

Sidebar

Related Questions

I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
I have two tables: **Product** ID Name SKU **Brand** ID Name Product table has
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have two tables in my MySQL database, one is a library of all
I have tables A and B with identity PKs ida and idb : ex
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity
I have tables named news and tags_news. Given a news id I have to
In the database we have tables called Sites, Organisations, Person, Department have the same

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.