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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:44:39+00:00 2026-06-08T19:44:39+00:00

I have three tables table name: tags id (int) tag_name (vharchar) table name: messages

  • 0

I have three tables

   table name: tags
          id (int)
          tag_name (vharchar)    

   table name: messages
          id (int)
          message_title (vharchar)
          message_content(vharchar)

    table name: message_tag
          message_id (int)
          tag_id (int)

I have tried this code but it didn’t work(returns empty)

SELECT messages.message_title, messages.message_content, tags.tag_name
            FROM messages
            INNER JOIN message_tag ON messages.id = message_tag.message_id
            INNER JOIN tags ON tags.id = message_tag.tag_id
            WHERE message_tag.tag_id =191
            AND message_tag.tag_id =19
            AND message_tag.tag_id =31
            ORDER BY RAND( )
            LIMIT 20

I want to select random 20 rows which are connected to tag 191, 19 and 31

  • 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-08T19:44:41+00:00Added an answer on June 8, 2026 at 7:44 pm
    SELECT messages.message_title, messages.message_content, tags.tag_name
            FROM messages
            INNER JOIN message_tag ON messages.id = message_tag.message_id
            INNER JOIN tags ON tags.id = message_tag.tag_id
            WHERE message_tag.tag_id IN (191, 19, 31)
            ORDER BY RAND( )
            LIMIT 20
    

    Use IN instead of AND. This will bring you messages which are linked to one at least of those particular tags.

    Update: IF you wanted to get messages where all 3 tags are present, you would do this instead (assuming your rows in message_tag are unique):

        SELECT messages.message_title, messages.message_content, tags.tag_name
            FROM messages
            INNER JOIN message_tag ON messages.id = message_tag.message_id
            INNER JOIN tags ON tags.id = message_tag.tag_id
            WHERE message_tag.tag_id IN (191, 19, 31)
            GROUP BY messages.id
            HAVING COUNT(messages.id) > 2
            ORDER BY RAND( )
            LIMIT 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables: articles(id int) tags(id int, name varchar(255)) articles_tags(article_id, tag_id) I want
I have three tables like this: Person table: person_id | name | dob --------------------------------
I have three tables, Name - columns Custpackingslipjour(table 1) - (deliverydate, dateclosed,, salesid) Inventrans
I have a table tags with fields tag_id and name . I have a
I have three tables in my database: Products id (int, primary key) name (varchar)
I working on a tag based search. I have three tables tag(id,name), tagXmedia(id,tag_id,media_id), and
Using MySQL, I have three tables: projects : ID name 1 birthday party 2
I have three tables filters (id, name) items(item_id, name) items_filters(item_id, filter_id, value_id) values(id, filter_id,
I have three tables Person (id, name, email) Address (id, Person_id, address, verified) Person_Claimed
I have three tables A: A.pID primary key, A.Name nvarchar(250) B: B.pID primary key,

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.