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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:09:31+00:00 2026-06-14T20:09:31+00:00

I’m still a SQL-rookie. I have three tables with a given structure: table1 (products)

  • 0

I’m still a SQL-rookie. I have three tables with a given structure:

table1 (products)
--------------------------------------
id   ident   title   types   desc
--------------------------------------
01   111ab   title1  2       details 1
02   222ab   title2  1       details 2
03   333ab   title3  2       details 3

A product can belong to many product-types. The “types”-column is the number of types related to a product. Product “111ab” belongs to 2 product-types.

table2 (product-types)
--------------------------
id   type    typedesc
--------------------------
01   type1   description 1
02   type2   description 2
03   type3   description 3


table3 (relations)
-------------------
id   tbl1id  tbl2id
-------------------
01   01      01
02   02      03
03   03      03
04   01      03
05   03      02

Table3 shows the relations between table1 (products) and table2 (product-types). Product “111ab” is related to the product-types “01” and “03”.

I want to create a data-structure which should be something like this:

type-description    prod   title   desc
-------------------------------------------
type1-description1  111ab  title1  details1
type2-description2  333ab  title3  details3
type3-description3  111ab  title1  details1
type3-description3  222ab  title2  details2
type3-description3  333ab  title3  details3

This structure will end up in an assoc-array of product-types with the related products as assoc-sub-arrays, sorted by “ident” of table1:

type1 description1
        111ab title1 details1

type2 description2
        333ab title3 details3

type3 description3
        111ab title1 details1
        222ab title2 details2
        333ab title3 details3

This array will be json-encoded and sent as a query-result of an ajax-request.

I read the threads about JOINing tables here at stackoverflow but can not generate a working SELECT-structure. Maybe this can not be done by a single query. I don’t know.

Please, can anyone give me a little help?

  • 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-14T20:09:32+00:00Added an answer on June 14, 2026 at 8:09 pm

    an INNER JOIN will suffice your requirements,

    SELECT  a.type, a.typedesc,
            c.ident, c.title,
            c.`desc`
    FROM    `product-types` a
            INNER JOIN relations b
                ON a.id = b.tbl2id
            INNER JOIN products c
                ON b.tbl1id = c.id
    ORDER BY a.type, a.typedesc
    
    • SQLFiddle Demo

    or

    SELECT  CONCAT(a.type, '-',a.typedesc) `type-description`,
            c.ident, c.title,
            c.`desc`
    FROM    `product-types` a
            INNER JOIN relations b
                ON a.id = b.tbl2id
            INNER JOIN products c
                ON b.tbl1id = c.id
    ORDER BY `type-description`
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.