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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:45:57+00:00 2026-06-14T07:45:57+00:00

I am working on an EAV storage system for storing schemaless meta data about

  • 0

I am working on an EAV storage system for storing schemaless meta data about an object, and am now working on a way to have it searchable, but have most of the processing done by the Database Server.

The table of the EAV is:

`entityID` - INT(11)
`entity` - VARCHAR(128)
`attribute` - VARCHAR(128)
`value` - BLOB

This is my statement:

SELECT 
    `entity`,
    (CASE WHEN `attribute` = 'bob' THEN `value` ELSE NULL END) `bob`,
    (CASE WHEN `attribute` = 'dean' THEN `value` ELSE NULL END) `dean`
FROM `eav`

which returns a nice set of rows (as expected) like this:

+----------+------+------+
|  entity  | bob  | dean |
+----------+------+------+
| testEnt  | foo  | NULL | // attribute = bob
+----------+------+------+
| testEnt  | NULL | NULL | // another test attribute
+----------+------+------+
| testEnt  | NULL | NULL | // another test attribute
+----------+------+------+
| testEnt2 | foo  | NULL | // attribute = bob
+----------+------+------+
| testEnt2 | NULL | foo  | // attribute = dean
+----------+------+------+

but when I attach GROUP BY (Entity), the result turns into this:

+----------+------+------+
|  entity  | bob  | dean |
+----------+------+------+
| testEnt  | foo  | NULL |
+----------+------+------+
| testEnt2 | foo  | NULL |
+----------+------+------+

so using the HAVING syntax after that stops working. Is there a way so that the result returned is:

+----------+------+------+
|  entity  | bob  | dean |
+----------+------+------+
| testEnt  | foo  | NULL |
+----------+------+------+
| testEnt2 | foo  | foo  |
+----------+------+------+
  • 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-14T07:45:58+00:00Added an answer on June 14, 2026 at 7:45 am

    Apply an aggregate function with the CASE and the GROUP BY. Since the values are strings you can use either a MAX() or MIN() to return the result:

    SELECT 
        `entity`,
        Max(CASE WHEN `attribute` = 'bob' THEN `value` ELSE NULL END) `bob`,
        Max(CASE WHEN `attribute` = 'dean' THEN `value` ELSE NULL END) `dean`
    FROM `eav`
    group by `entity`
    

    See SQL Fiddle with Demo

    In the event, you have an unknown number of attribute values to return, you can use a prepared statement to get the result:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'max(case when attribute = ''',
          attribute,
          ''' then value end) AS ',
          attribute
        )
      ) INTO @sql
    FROM eav;
    
    SET @sql = CONCAT('SELECT entity, ', @sql, ' 
                      FROM eav 
                       GROUP BY entity');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See SQL Fiddle with Demo

    The result for both will be:

    |   ENTITY | BOB |   DEAN |
    ---------------------------
    |  testEnt | foo | (null) |
    | testEnt2 | foo |    foo |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a billing system for service provider and I have to
I have my log file working, but I get extraneous info on each line
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
Working with JSF 2.0 (and Primefaces), is there a way to fire an ActionListener
I am working in a new Filter by Product module in Magento, i have
What is the best way to export data from multiple tables in MySQL. I'm
Working on an image gallery, but it keeps crashing on me (you control it
Working through C++ Primer Plus and am trying to cin data to a dynamically
Working in the JBoss AS portlet we have been unable to open a jsp
I'm used to working with mysql but for my next series of projects CouchDB

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.