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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:24:46+00:00 2026-05-21T11:24:46+00:00

Say I have 2 tables, one called categories and one called cat_pages. The categories

  • 0

Say I have 2 tables, one called categories and one called cat_pages.

The categories table has columns ID, title and timestamp. For example:

CREATE TABLE categories (
    id INT UNSIGNED PRIMARY KEY,
    title VARCHAR(32),
    `timestamp` TIMESTAMP,
    INDEX (title)
) Engine=InnoDB;

The cat_pages has 2 columns, cat_id and page_id:

CREATE TABLE cat_pages (
    cat_id INT UNSIGNED 
        REFERENCES categories (id) 
        ON DELETE CASCADE ON UPDATE CASCADE,
    page_id INT UNSIGNED
        REFERENCES pages (id) 
        ON DELETE CASCADE ON UPDATE CASCADE,
    UNIQUE INDEX (cat_id, page_id),
    INDEX (page_id, cat_id),
) Engine=InnoDB;

I’m trying to join the categories table with the cat_pages table on the ID, such that

  1. Only categories with id’s in the category_pages table are retrieved and
  2. Each category is only displayed once in the resultset

The query:

SELECT * FROM categories as c
    LEFT JOIN cat_pages as p ON c.id = p.cat_id

produces a result set that has the categories repeated multiple times (as there are multiple matches in the cat_pages table. What do I need so that each category is only shown once, and not at all if there are no matches in the cat_pages table?

  • 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-21T11:24:47+00:00Added an answer on May 21, 2026 at 11:24 am

    If you don’t want categories that aren’t in cat_pages, don’t use a left join; use an inner join. A left join includes every row from the left table, even if there isn’t a matching row in the right table (the missing fields are given NULL values). A right join is similar, but includes all rows from the right table. An outer join includes all rows from the left and right tables, joining rows that have matches and joining rows without matches with NULL values. An inner join, by contrast, only includes matching rows. To put it another way, the intersection of left and right joins is an inner join; their union is an outer join. Jeff Atwood posted some nice Venn diagrams describing joins, though it should be noted that the sets in the circles aren’t properly the left and right tables, but rather the results of the left and right joins of the left and right tables.

    To get distinct rows, you can use a DISTINCT modifier:

    SELECT DISTINCT c.* 
      FROM categories AS c 
        INNER JOIN cat_pages AS cp ON c.id = cp.cat_id
    

    As for SELECT * ..., see “What is the reason not to use select *?“

    Another approach to getting distinct rows would be to use an EXISTS clause or IN operator, but the join is likely more performant (though only an EXPLAIN would tell you for certain). Just make sure you have appropriate indices set.

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

Sidebar

Related Questions

Lets say I have one table called REVIEWS This table has Reviews that customers
Let's say I have two tables one called Customers and one called CustomerAddresses. Then
I have 3 tables in a MySQL database. Table 1 (say it's called client
i have 2 tables 1st table called services has id_service, name, date. description 2nd
say for example i have a tables called table1 and table2; table1{q_id,q_text,date}, table2{a_id,q_id,a_text} I
Lets say I have data in two tables. In one I have Order ID
Lets say I have a Customers table and an Orders table with a one-to-many
Say you have a MySQL 5.0 MyISAM table with 100 million rows, with one
Say I have three tables, a table of users, a table of around 500
Say I have two tables called A (fields: id, phase, name) and B(fields: id,

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.