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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:55:18+00:00 2026-05-29T08:55:18+00:00

I have a table like this: ID | name ================================= 1 | apple 1

  • 0

I have a table like this:

ID | name
=================================
 1 | apple
 1 | bear
 2 | cactus
 2 | dog
 2 | apple
 3 | apple
 3 | dog
==================================

and I wish to get the following unique set of combinations per ID as output from an SQL query:

===================
apple  | bear
apple  | cactus
apple  | dog
cactus | dog
===================

It’s essentially a type of transpose. Ideally, I’d like to expand this to include a count of co-occurances, like this:

===================
1 | apple  | bear
1 | apple  | cactus
2 | apple  | dog
1 | cactus | dog
===================

but this may be above and beyond. I know I can accomplish all of the above using a stored proc; was just curious if I can do this as a query.

Any SQL will do, but MySQL would be preferred if possible.

Thanks for any and all input!

  • 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-29T08:55:19+00:00Added an answer on May 29, 2026 at 8:55 am

    From your example, it looks like you want the first column to contain whatever name comes first alphabetically, so I used a < when joining the table to itself.

    select count(*), t1.Name, t2.Name
    from 
        tblThings t1 join
        tblThings t2 ON t1.ID = t2.ID AND t1.Name < t2.Name
    group by t1.Name, t2.Name
    

    And here’s the query in action to prove I’m not crazy.

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

Sidebar

Related Questions

I have a table like this: name code group john 12 smith 15 how
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have a table like this (simplified): ID | Name | Parent --------------------------------- 1
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have a table where a record looks like this varchar(255) Name varchar(255) Text
I have a table like this table name: products +----+-------------+-----------+ | id | name

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.