开发者

MySQL Dynamically Merge/Select Select Rows

开发者 https://www.devze.com 2023-02-03 12:26 出处:网络
I just need some direction on this.I have the following tables: Table: entity - ID (INT) Table: attributes

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!


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.


I do not condone selecting * so replace with your columns. Values may be a reserved keyword

   select * 
   from values v

   left join attributes a
     on a.id = v.attributes

   left join entity e
     on e.id = v.entity

   where 1=1

   /* put your where clause here */
0

精彩评论

暂无评论...
验证码 换一张
取 消