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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:29:35+00:00 2026-05-13T20:29:35+00:00

I need some help on a MySQL query I’m trying to set up. I

  • 0

I need some help on a MySQL query I’m trying to set up. I need to find records that match conditions that are located in two different tables with a many to many relationship.

I use three tables in this query, the first contains projects, the second contains topics and the third ties them together. I want the query to find projects that are related to the topic that was selected by the user. Users can also select more than one topic to perform the search. In that case I only want to show projects that are related to both topics and not to either of them. This is what I can’t figure out how to do. I expected that I’d have to do something like this but this query yields no results whilst there is a project that is linked to both topics in the database.

SELECT `projects`.*
FROM `projects`, `topics`, `projects_topics`
WHERE (`name` LIKE '%%' || `vision` LIKE '%%' || `highlights` LIKE '%%' || `optional` LIKE '%%')
    && ((`projects_topics`.`projects_id` = `projects`.`id` && `projects_topics`.`topics_id` = `topics`.`id` && `topics`.`id` = '1')
        && (`projects_topics`.`projects_id` = `projects`.`id` && `projects_topics`.`topics_id` = `topics`.`id` && `topics`.`id` = '9'))
ORDER BY `date_added` DESC

These are the tables:

projects

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment | 
| fields_id    | int(11)      | NO   | PRI | NULL    |                | 
| name         | varchar(255) | YES  |     | NULL    |                | 
| address      | varchar(255) | YES  |     | NULL    |                | 
| zip          | varchar(255) | YES  |     | NULL    |                | 
| city         | varchar(255) | YES  |     | NULL    |                | 
| state        | varchar(255) | YES  |     | NULL    |                | 
| countries_id | int(11)      | NO   | PRI | NULL    |                | 
| website      | varchar(255) | YES  |     | NULL    |                | 
| client       | varchar(255) | YES  |     | NULL    |                | 
| finished     | date         | YES  |     | NULL    |                | 
| budget       | int(11)      | YES  |     | NULL    |                | 
| vision       | text         | YES  |     | NULL    |                | 
| highlights   | text         | YES  |     | NULL    |                | 
| innovation   | text         | YES  |     | NULL    |                | 
| optional     | text         | YES  |     | NULL    |                | 
| publish      | tinyint(1)   | YES  |     | NULL    |                | 
| date         | datetime     | YES  |     | NULL    |                | 
| featured     | tinyint(1)   | YES  |     | NULL    |                | 
| frontpage    | tinyint(1)   | YES  |     | NULL    |                | 
| date_added   | datetime     | YES  |     | NULL    |                | 
+--------------+--------------+------+-----+---------+----------------+

topics

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment | 
| topic | varchar(255) | YES  |     | NULL    |                | 
| order | int(11)      | YES  |     | NULL    |                | 
+-------+--------------+------+-----+---------+----------------+

projects_topics

+-------------+---------+------+-----+---------+-------+
| Field       | Type    | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| projects_id | int(11) | NO   | PRI | NULL    |       | 
| topics_id   | int(11) | NO   | PRI | NULL    |       | 
+-------------+---------+------+-----+---------+-------+
  • 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-13T20:29:36+00:00Added an answer on May 13, 2026 at 8:29 pm
    SELECT  p.*
    FROM    (
            SELECT  project_id
            FROM    project_topics pt
            WHERE   topics_id IN (5, 9)
            GROUP BY
                    project_id
            HAVING  COUNT(*) = 2
            ) pto
    JOIN    projects p
    ON      p.project_id = pto.project_id
    

    or

    SELECT  p.*
    FROM    projects p
    WHERE   EXISTS
            (
            SELECT  NULL
            FROM    project_topics pt
            WHERE   pt.project_id = p.project_id
                    AND pt.topic_id IN (5, 9)
            LIMIT 1 OFFSET 1
            )
    

    Make sure that the PK in project_topics is defined as (project_id, topic_id) (in this order), or create an additional UNIQUE index on (topic_id, project_id).

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

Sidebar

Related Questions

I need some help with a MySQL query. I have two tables, one with
I need some help with a MySQL query. I'm trying to rank participants using
Need some help with putting this query together. I'm using Mysql I have two
I really need some help with forming a MySQL query that I just cannot
I need some help with a MySQL query I'm working on. I have data
I need some help optimizing a MySQL query or table When I run this
i need some help with this sql query heres my mysql table structure: DOMAINS
I am trying to optimize my MySQL queries and I need some help. Here
I need some help with a group by mysql query clause. Medals Table (this
I need some help creating a query for my mySQL database. I have recently

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.