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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:55:36+00:00 2026-05-19T09:55:36+00:00

I just need some direction on this. I have the following tables: Table: entity

  • 0

I just need some direction on this. I have the following tables:

Table: entity
 - ID (INT)

Table: attributes
 - ID (INT)
 - name (VARCHAR)
 - internalname (VARCHAR)

Table: values
 - ID (INT)
 - entity (ID)
 - attributes (INT)
 - value (Text)

What I want is to make a Select statment that will return something like the following:

 - ID = entity.ID
 - {attributes.internalname ID 1} = values.{attribe internalname 1}.value
 - {attributes.internalname ID 2} = values.{attribe internalname 2}.value
 - {attributes.internalname ID 3} = values.{attribe internalname 3}.value
 - {attributes.internalname ID n} = values.{attribe internalname n}.value
 - etc...

It would be like combining:

SELECT entity.id FROM entity;

and

SELECT (SELECT values.value FROM values WHERE values.entity = entity.ID AND values.attributes = attributes.ID) FROM attributes;

It is a difficult thing to explain, however if you need me to explain further, please let me know.

I effectively want to rotate all values in attributes to columns and turn all the values into its corresponding attribute’s value with the ID as the selector.

I give the query an ID (The element ID), and in one result row it returns all the data.

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-19T09:55:37+00:00Added an answer on May 19, 2026 at 9:55 am

    You can’t create columns dynamically, so you need to know beforehand what you want as columns.

    If attributes (1,2,3,4) represent (firstname, lastname, extraname, additionalname) you can query it like this:

    select e.id
          ,v1.value as firstname
          ,v2.value as lastname
          ,v3.value as extraname
          ,v4.value as additionalname
      from entity e
      left join values v1 on(e.id = v1.entity and v1.attributes = 1)
      left join values v2 on(e.id = v2.entity and v2.attributes = 2)
      left join values v3 on(e.id = v3.entity and v3.attributes = 3)
      left join values v4 on(e.id = v4.entity and v4.attributes = 4)
     where e.id = ?
    

    or

    select e.id
          ,max(case when v.attributes = 1 then value) as firstname
          ,max(case when v.attributes = 2 then value) as lastname
          ,max(case when v.attributes = 3 then value) as extraname
          ,max(case when v.attributes = 4 then value) as additionalname
      from entity e
      left join values v on(e.id = v.entity)
     where v.attributes in(1,2,3,4)
       and e.id = ?
     group by e.id;
    

    You can also use the GROUP_CONCAT to return the values in a comma separated list in one column.

    select e.id
          ,group_concat(v.value)
      from entity e
      left join values v on(e.id = v.entity)
     group 
        by e.id;
    

    Oh, and values is a reserved word. Don’t use it as table name.

    Oh 2, don’t use this model unless you really really have to. You will pay a big juicy price in terms of performance and data consistency.

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

Sidebar

Related Questions

I need some help with jQuery script again :-) Just trying to play with
I'm just starting out with the whole ajax thing and I need some help.
I need to serialize/de-serialize some objects into/from string and transfer them as just opaque
This code does not seem to compile, I just need to write something to
Seems so basic, I can't believe I don't know this! I just need a
I need to create a midnight DateTime I've just done this: DateTime endTime =
I just need a file (picture, pdf or other type file for printing) of
I need to get just the first item (actually, just the first key) off
I need a collection that works just like a Dictionary/Hashtable in a sense that
I need to store items with a calendar date (just the day, no time)

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.