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

The Archive Base Latest Questions

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

I have two tables. One is items , and one is tags . Query’s

  • 0

I have two tables. One is items, and one istags. Query’s here.

The idea is to create a search box to search through the items and then narrow down the search by selecting tags. (Some kind of faceted search).

After fiddling around with mysql tutorials i came up with this:

SELECT `items`.`id`, `items`.`name`, `items`.`pay`, `items`.`desc`
FROM `items`LEFT JOIN (`tags`) ON (`tags`.`item_id`=`items`.`id`) 
WHERE (
    (`tags`.`type`='food' AND `tags`.`name`='pizza')
    OR (`tags`.`type`='drinks' AND `tags`.`name`='lemonade')
    --And so on for every tag
)
ORDER BY `pay` DESC LIMIT 0 , 30

(I removed the full-text search in the query because it isn’t relevant right now)

It works pretty good, but I have two problems:

  1. If the user doesn’t select any tags, every item that has more then 1 tag attached to it shows up more then 1 time
  2. If the user selects two tags, food=>pizza and drinks=>lemonade by example, every item with pizza or lemonade or both will show up, I only want the items that have BOTH those tags attached to it.

Is it possible to solve these problems with a different mysql query? Or should I fix these problems in the php code. (Or am I doing this totally wrong, and is there a much better way to use tags?)

Hopefully I made myself clear.

Gr,

Bas

  • 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:01:17+00:00Added an answer on May 13, 2026 at 8:01 pm

    Throw in the keyword DISTINCT after SELECT?

    SELECT DISTINCT `items`.`id`, `items`.`name`, `items`.`pay`, `items`.`desc` 
    FROM `items`LEFT JOIN (`tags`) ON (`tags`.`item_id`=`items`.`id`)  
    WHERE ( 
        (`tags`.`type`='food' AND `tags`.`name`='pizza') 
        OR (`tags`.`type`='drinks' AND `tags`.`name`='lemonade') 
        --And so on for every tag 
    ) 
    ORDER BY `pay` DESC LIMIT 0 , 30 
    

    As for your second issue… your logic is just wrong. If I understand you correctly you want to group by items.id and then reject the items that don’t come back with the same number of rows as the number of selected tags… maybe something like:

    SELECT DISTINCT `items`.`id`, `items`.`name`, `items`.`pay`, `items`.`desc` 
        FROM `items`LEFT JOIN (`tags`) ON (`tags`.`item_id`=`items`.`id`)  
        WHERE ( 
            (`tags`.`type`='food' AND `tags`.`name`='pizza') 
            OR (`tags`.`type`='drinks' AND `tags`.`name`='lemonade') 
            --And so on for every tag 
        ) 
        GROUP BY `items`.`id` 
        HAVING COUNT(*) = 2 
        ORDER BY `pay` DESC LIMIT 0 , 30 
    

    … and replace the 2 in HAVING COUNT(*) = 2 with the number of tags you are trying to concurrently match …

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

Sidebar

Ask A Question

Stats

  • Questions 314k
  • Answers 314k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I guess, you may need to create a custom Appender… May 13, 2026 at 11:08 pm
  • Editorial Team
    Editorial Team added an answer Since you're using jQuery and the solution isn't, you could… May 13, 2026 at 11:08 pm
  • Editorial Team
    Editorial Team added an answer From Brian Henderson on the MSDN forums: Silverlight runs in… May 13, 2026 at 11:08 pm

Related Questions

I have a question: I have two MSSQL tables, items and states, that are
I have two tables, one called cart and one called items. Now I want
I have a database that has two tables, one of which contains a foreign
I have a purchase order table and another table to contain the items within
Coming from another question of mine where I learnt not to EVER use db

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.