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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:09:44+00:00 2026-05-16T06:09:44+00:00

I have this query that should display all posts related to a specific tag

  • 0

I have this query that should display all posts related to a
specific tag but for some reason it will only display one result instead of all the results can some one help me fix this problem?

Here is MySQL code.

"SELECT users.*, users_posts.*, tags.*, posts_tags.*, 
FROM users
INNER JOIN users_posts ON users_posts.user_id = users.user_id 
INNER JOIN posts_tags ON users_posts.id = posts_tags.posts_id
INNER JOIN tags ON posts_tags.tag_id = tags.id
WHERE tags.tag = '" . $tag_id . "'
GROUP BY tags.tag"

Here is my MySQL table.

CREATE TABLE posts_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag_id INT UNSIGNED NOT NULL,
users_posts_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE users_posts (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
content TEXT NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
pass CHAR(40) NOT NULL,
PRIMARY KEY (user_id)
);
  • 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-16T06:09:44+00:00Added an answer on May 16, 2026 at 6:09 am

    I added some test data to your tables:

    INSERT INTO tags VALUES (1, 'mysql');
    INSERT INTO tags VALUES (2, 'javascript');
    INSERT INTO tags VALUES (3, 'c++');
    
    INSERT INTO users VALUES (1, 'user a', 'pass');
    INSERT INTO users VALUES (2, 'user b', 'pass');
    INSERT INTO users VALUES (3, 'user c', 'pass');
    
    INSERT INTO users_posts VALUES (1, 1, 'some content 1');
    INSERT INTO users_posts VALUES (2, 1, 'some content 2');
    INSERT INTO users_posts VALUES (3, 2, 'some content 3');
    INSERT INTO users_posts VALUES (4, 2, 'some content 4');
    INSERT INTO users_posts VALUES (5, 2, 'some content 5');
    INSERT INTO users_posts VALUES (6, 3, 'some content 6');
    
    INSERT INTO posts_tags VALUES (1, 1, 1);
    INSERT INTO posts_tags VALUES (2, 2, 1);
    INSERT INTO posts_tags VALUES (3, 1, 2);
    INSERT INTO posts_tags VALUES (4, 3, 2);
    INSERT INTO posts_tags VALUES (5, 2, 3);
    INSERT INTO posts_tags VALUES (6, 2, 4);
    INSERT INTO posts_tags VALUES (7, 3, 4);
    INSERT INTO posts_tags VALUES (8, 1, 5);
    INSERT INTO posts_tags VALUES (9, 2, 6);
    INSERT INTO posts_tags VALUES (10, 3, 6);
    

    Then removing the GROUP BY:

    SELECT      *
    FROM        users
    INNER JOIN  users_posts ON users_posts.user_id = users.user_id
    INNER JOIN  posts_tags ON users_posts.id = posts_tags. users_posts_id
    INNER JOIN  tags ON posts_tags.tag_id = tags.id
    WHERE       tags.tag = 'mysql';
    

    Returns:

    +---------+--------+------+----+---------+----------------+----+--------+----------------+----+-------+
    | user_id | name   | pass | id | user_id | content        | id | tag_id | users_posts_id | id | tag   |
    +---------+--------+------+----+---------+----------------+----+--------+----------------+----+-------+
    |       1 | user a | pass |  1 |       1 | some content 1 |  1 |      1 |              1 |  1 | mysql |
    |       1 | user a | pass |  2 |       1 | some content 2 |  3 |      1 |              2 |  1 | mysql |
    |       2 | user b | pass |  5 |       2 | some content 5 |  8 |      1 |              5 |  1 | mysql |
    +---------+--------+------+----+---------+----------------+----+--------+----------------+----+-------+
    3 rows in set (0.00 sec)
    

    It’s just one row per post, as long as the posts are not tagged with the same tag more than once. In fact, to prevent this from happening, you may want to consider eliminating the surrogate key in posts_tags and use a composite primary key on (tag_id, users_posts_id):

    CREATE TABLE posts_tags (
       tag_id INT UNSIGNED NOT NULL,
       users_posts_id INT UNSIGNED NOT NULL,
       PRIMARY KEY (tag_id, users_posts_id)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query in Data Access Object DAOComments that joins users table and
I've checked most of the question that was display regarding my title but they
I'm using jquery and php for some async job. I'm using $.ajax with beforesend
So, here's the scenario : I have a page in my asp.net c# based
I am building a social app that has a chatView. Message is an entity.
I am using SL 4, WCF RIA Services against Entity Framework 4.0. I have
I am scraping data from I want to scrape three search engines. In My
I'm modifying the Coldfusion-based interface for a listserv admin application to show snippits of
I am struggling to get my custom drawing code to render at the proper
Starting with MVC2, messing around with a simple db and just using the index

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.