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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:10:14+00:00 2026-05-21T11:10:14+00:00

I have a very annoying problem on the application I’ve been contracted to work

  • 0

I have a very annoying problem on the application I’ve been contracted to work in.

The database structure, which I can’t modify for compatibility reasons is a mess. It’s basically a many-to-many-to-many-to-many-to-bang-your-head-on-the-desk relationship. It goes like this:

contact

---------------------------
| contact_id | name | ... |
---------------------------
| 1          | foo  |     |
---------------------------

metadefinition

-----------------------------
| metadefinition_id | name  |
-----------------------------
| 1                 | title |
-----------------------------
| 2                 | job   |
-----------------------------

contact_metadata

----------------------------
| contact_id | metadata_id |
----------------------------
| 1          | 1           |
----------------------------
| 1          | 2           |
----------------------------

metadata

-------------------------------------------
| metadata_id | metadefinition_id | value |
-------------------------------------------
| 1           | 1                 | mrs   |
-------------------------------------------
| 2           | 2                 | coder |
-------------------------------------------

So, for a single contact, I want to search on all this and obtain something like this:

-----------------------------------------------------
| contact_id | name | metadata.title | metadata.job |
-----------------------------------------------------
| 1          | foo  | mrs            | coder        |
-----------------------------------------------------

So now what I’ve tried so far. I can fetch the list of metadefinition in advance, that’s not really an issue. So I build a query like this:

SELECT contact.*,m1.value AS `metadata.title` FROM contact
   LEFT JOIN contact_metadata ON contact.contact_id = contact_metadata.contact_id
   LEFT JOIN metadata m1 ON contact_metadata.metadata_id = m1.metadata_id AND m1.metadefinition_id = 1
   GROUP BY contact_id

This works for a single metadefinition, I get something like this:

--------------------------------------
| contact_id | name | metadata.title |
--------------------------------------
| 1          | foo  | mrs            |
--------------------------------------

If I try with two, however:

SELECT contact.*,m1.value AS `metadata.title`,m2.value AS `metadata.job` FROM contact
   LEFT JOIN contact_metadata ON contact.contact_id = contact_metadata.contact_id
   LEFT JOIN metadata m1 ON contact_metadata.metadata_id = m1.metadata_id AND m1.metadefinition_id = 1
   LEFT JOIN metadata m2 ON contact_metadata.metadata_id = m2.metadata_id AND m2.metadefinition_id = 2
   GROUP BY contact_id

I get:

-----------------------------------------------------
| contact_id | name | metadata.title | metadata.job |
-----------------------------------------------------
| 1          | foo  | mrs            | NULL         |
-----------------------------------------------------

If I remove the GROUP BY clause, of course, I get:

-----------------------------------------------------
| contact_id | name | metadata.title | metadata.job |
-----------------------------------------------------
| 1          | foo  | mrs            | NULL         |
-----------------------------------------------------
| 1          | foo  | NULL           | coder        |
-----------------------------------------------------

I’m open to anything as long as the query times are relatively acceptable (considering the structure, if it takes 10 seconds for 100000 records it’s better than nothing)

Will it be temporary tables, stored procedures, I don’t care, as long as I don’t have to change the actual database structure.

Trust me, when it’ll be the time to remodel, it’ll be my pleasure to take this whole thing down.

Is there a solution to this, is it possible?

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-21T11:10:15+00:00Added an answer on May 21, 2026 at 11:10 am

    I’d use a pair of correlated sub-queries. Something like

    SELECT c.*
      ,( Select value from metadata m1 
           INNER JOIN contact_metadata cm 
           on cm.metadata_id = m1.metadata_id 
         WHERE m1.metadefinition_id = 1 
           AND cm.contact_id = c.contact_id )AS `metadata.title`
      ,( Select value from metadata m1 
           INNER JOIN contact_metadata cm 
           ON cm.metadata_id = m1.metadata_id 
         WHERE m1.metadefinition_id = 2 
           AND cm.contact_id = c.contact_id )AS `metadata.job`
    FROM contact c
    WHERE c.contact_id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very annoying exception which freeze my application thrown by WPF, when
I have a very annoying problem. I have a mIRC/Skype application, meaning chatroom with
I have a very annoying problem which is my z-index is not working and
I am having a very annoying problem. I have an application with a setup
I've recently started using GNU Screen but have run into a very annoying problem.
I have a layout that is working, but it has one very annoying problem..
I have a very annoying problem and I'm trying to find the simplest solution
I've got a very annoying problem with my Zend studio. I have a zend
We've got this very annoying problem with Scriptaculous and Internet Explorer 7/8. We have
I have a very annoying problem with jQuery and the datepicker from jQuery-UI. Here

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.