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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:56:45+00:00 2026-05-13T06:56:45+00:00

I have 2 tables. Table 1 is ‘articles’ and Table 2 is ‘article_categories’. When

  • 0

I have 2 tables. Table 1 is ‘articles’ and Table 2 is ‘article_categories’. When a user creates an article, it is stored into ‘articles’. The user, while creating the article can select various categories under which this article can appear. Currently, an article can be selected to belong to anywhere from 10-25 categories(may be increased in future). These Categories under which the article is filed, are stored in ‘article_categories’. So this means a single article ID can have multiple related values in table ‘article_categories’. When retrieving all the values from both the tables, I would need the all the values from ‘article_categories’ to be pulled and the values to be stored in an array.

My question is about what SQL query to use in order to do so? Should I use Inner Join, Left Join, Outer Join…? What would be the best way to do that? I did try some of those joins in phpmyadmin and they give me repeating values of the same article, when in fact, the article should be fetched only once and all the related categories to be fetched. I wanted to do this all in the same query without having to split the query into 2 different in order to accomplish this. I am attaching my table structures so that it’s easy for you:

CREATE TABLE IF NOT EXISTS `articles` (
  `article_id` int(11) unsigned NOT NULL auto_increment,
  `creator_id` int(11) unsigned NOT NULL,
  `article_title` varchar(150) NOT NULL,
  `article_status` varchar(10) NOT NULL,
  PRIMARY KEY  (`article_id`),
  KEY `buyer_id` (`creator_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `articles`
--

INSERT INTO `articles` (`article_id`, `creator_id`, `article_title`, `article_status`) VALUES
(1, 1, 'My article 1', 'Pending');


CREATE TABLE IF NOT EXISTS `article_categories` (
  `article_id` int(11) unsigned NOT NULL,
  `category_id` smallint(3) unsigned NOT NULL,
  PRIMARY KEY  (`article_id`,`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `article_categories`
--

INSERT INTO `article_categories` (`article_id`, `category_id`) VALUES
(1, 1),
(1, 2),
(1, 3),
(1, 4),
(1, 5),
(1, 36),
(1, 71);

Also please note that I have a composite key on the article_id and category_id keys in article_categories table. A sample query that I used is below:

SELECT *
FROM articles, article_categories 
WHERE articles.article_id = article_categories.article_id
AND articles.article_id = 1;

This results in:

article_id  creator_id  article_title   article_status  article_id  category_id
    1   1   My article 1    Pending 1   1
    1   1   My article 1    Pending 1   2
    1   1   My article 1    Pending 1   3
    1   1   My article 1    Pending 1   4
    1   1   My article 1    Pending 1   5
    1   1   My article 1    Pending 1   36
    1   1   My article 1    Pending 1   71

As can be seen, the value from the articles table is repeating and it’s also able to get all the categories(it’s the last column, in case the formatting is messed up). I wanted to get the values from the articles table only once and get the category_id in a loop, so that I can add those looped values in an array and carry on my processing. This is what I intend to do after fetching the values from above:

<?php
//i wanted to check if the article_id exists before i pull the related categories. 
//If I do it this way and output using mysql_num_rows, it gives me value 7,
//when in fact, the there's only 1 article with such Id. This means it also counts
//  the number of categories. Is there a way to uniquely identify only the number of
// articles (just to see if it exists or not, in the place)

$result = mysql_query("SELECT *
FROM articles, article_categories 
WHERE articles.article_id = article_categories.article_id
AND articles.article_id = 1");

while ( $rows = mysql_fetch_array($result) )
    {   //i don't think this the following 2 assignments should be done in the loop
        $article_id = $rows['article_id'];
        $article_title = $rows['article_title'];

        //(loop all the category_id from the 2nd table and add to the array below)
        $categories_id[] .= ??????? --> How do i do this?       
    }   

?>

Obviously, I cannot do a LIMIT 1 on the above as that will limit my ability to retrieve all the category IDs.

So my question would be how do I get all the category_id from the 2nd table (in a loop) and add them to the array and at the same time, make sure that the values from table 1 are fetched only once (I do realize that the values fetched from the table 1 are the same but does not make sense to loop on them). To achieve this, I would like to get your input on what kind of Join I should use to execute the query with maximum efficiency and use the minimum resources, all in a single query to minimize hits on the DB. I hope that make sense.

Thanks in advance.

  • 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-13T06:56:45+00:00Added an answer on May 13, 2026 at 6:56 am

    EDIT:

    SELECT articles.article_id, articles.article_title, GROUP_CONCAT(article_categories.category_id SEPARATOR ',') as category_id
    FROM articles
    LEFT JOIN article_categories ON  (articles.article_id = article_categories.article_id)
    -- WHERE CLAUSE IF YOU WANT/NEED --
    GROUP BY articles.article_id;
    

    EDIT:
    Added column alias for group concat GROUP_CONCAT(article_categories.category_id SEPARATOR ',') as category_id

    <?php
    $result = mysql_query("SELECT articles.article_id, articles.article_title, GROUP_CONCAT(article_categories.category_id SEPARATOR ',') as category_id
    FROM articles, article_categories
    WHERE articles.article_id = 1
    GROUP BY articles.article_id;");
    
    while ( $rows = mysql_fetch_array($result) )
        {   
            $article_id = $rows['article_id'];
            $article_title = $rows['article_title'];
    
            //(loop all the category_id from the 2nd table and add to the array below)
            $categories_id = explode(',', $rows['category_id']);               
        }   
    
    ?>
    

    Beware of group concat though as it does have limit:

    The result is truncated to the maximum length that is given by the
    group_concat_max_len system
    variable, which has a default value of
    1024. The value can be set higher,
    although the effective maximum length
    of the return value is constrained by
    the value of max_allowed_packet.

    EDIT:
    Also without using the group concat i would go ahead and do it the way you had it… just make the category id your primary looping stcuture:

    <?php
    $result = mysql_query("SELECT articles.article_id, articles.article_title, article_categories.category_id
    FROM articles, article_categories
    WHERE articles.article_id = 1");
    
    $articles = array();
    while ( $rows = mysql_fetch_array($result) )
        {   
            if(!array_key_exists($rows['article_id'], $articles)
            {
               $articles[$rows['article_id']] = array(
                   'article_id' => $rows['article_id'],
                   'article_title' => $rows['article_title']
                   'categories_id' => array()
                );
             }
    
             $articles[$rows['article_id']][] = $rows['categories_id'];             
        }   
    
    ?>
    

    This way you only query once, bur you would then have to loop over the article for the operation on the articles’ data.

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

Sidebar

Ask A Question

Stats

  • Questions 342k
  • Answers 343k
  • 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 Apparently it looks like, there is a way to tell… May 14, 2026 at 5:19 am
  • Editorial Team
    Editorial Team added an answer Oracle already does it's work in memory - disk I/O… May 14, 2026 at 5:19 am
  • Editorial Team
    Editorial Team added an answer I discovered my issue and it was a stupidity thing.… May 14, 2026 at 5:19 am

Related Questions

I have 2 tables in SQL: Table 1 Step Id Step Name Table 2
I have 2 tables. 1 is music and 2 is listenTrack. listenTrack tracks the
i have 2 tables(result of two separate SQL queries and this result will be
I have 2 tables, using an inner join to query them. SELECT COUNT(table2.id) FROM
I have 2 tables: 1) table Masterdates which contains all dates since Jan 1,

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.