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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:50:29+00:00 2026-06-17T05:50:29+00:00

I believe this is a pretty simple thing, and I swear I’ve done it

  • 0

I believe this is a pretty simple thing, and I swear I’ve done it before but I can’t remember how.

So let’s say I have a one-to-many relationship. I want to JOIN the two tables, but not allow duplicates for the left table.

SQLFIDDLE

So based on the above SQLFiddle, my results would be:

categories.title  |  items.NAME  |  items.category_id
-----------------------------------------------------
red               | apple        | 1
red               | car          | 1
red               | paper        | 1
yellow            | lego         | 2
yellow            | banana       | 2
blue              | pen          | 3

I want it to be:

categories.title  |  items.NAME  |  items.category_id
-----------------------------------------------------
red               | apple        | 1
NULL              | car          | 1
NULL              | paper        | 1
yellow            | lego         | 2
NULL              | banana       | 2
blue              | pen          | 3

My reasoning is that this way, I can easily loop over the results without having to do any further processing with PHP.

  • 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-17T05:50:30+00:00Added an answer on June 17, 2026 at 5:50 am

    You can replace the values with something like this:

    select 
      case when rownum = 1 then title else null end title,
      name,
      category_id
    from
    (
      SELECT c.title, 
        i.name, 
        i.category_id,
        @row:=(case when @prev=title and @precat=category_id 
               then @row else 0 end) + 1 as rownum,
        @prev:=title ptitle,
        @precat:=category_id pcat
      FROM items AS i
      INNER JOIN categories AS c 
        ON c.id = i.category_id
       order by i.category_id, c.title
    ) src
    order by category_id, rownum
    

    See SQL Fiddle with Demo

    The result is:

    |  TITLE |   NAME | CATEGORY_ID |
    ---------------------------------
    |    red |  apple |           1 |
    | (null) |    car |           1 |
    | (null) |  paper |           1 |
    | yellow |   lego |           2 |
    | (null) | banana |           2 |
    |   blue |    pen |           3 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe this is possible but I haven't done it before. I need to
A long explanation but I believe this is a pretty simple question. I think
I believe this is a fairly simple question but it is something I am
This i believe is pretty simple maybe im just looking at it differently, i
I believe my question is pretty simple so this should be a piece of
This is probably pretty simple for most... I have this line in Magento which
I have a question that's so simple I cannot believe I can't answer it
I believe this question might have been previously attempted in 2006 on a different
I believe this question applies to any of the For Html helpers, but my
I believe this questions has been asked in some or the other way but

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.