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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:28:21+00:00 2026-05-16T22:28:21+00:00

I have a database where I store keywords grouped into projects and data related

  • 0

I have a database where I store keywords grouped into projects and data related to each keyword, then I display datagrids foreach project with one row per keyword and several columns all retrieved from the same table “data”. I have 4 tables, keywords, projects, group_keywords and data. “keywords” only stores the keyword, “projects” the project name, “group_keywords” the keywords ids for the keywords assigned to that project, and “data” is where all the data foreach keyword goes, identified by a foreign key for the keywords.id, and a name column to identify the data name.

Now to retrieve the keywords + all the data for a project I use this query:

SELECT * FROM `group_keywords` 
INNER JOIN keywords on keywords.id = keyword_id 
INNER JOIN data ON data.id = keywords.id 
WHERE `group_id` = (SELECT `id` FROM `projects` WHERE `name` = 'ProjectName'

This gives me something like

    id  group_id    keyword_id  id  keyword     id  name    value
    12  5   52  52  absorption food     52  data_name_x1    6
    12  5   52  52  absorption food     52  data_name_x2    8
    12  5   52  52  absorption food     52  data_name_x3    26
    12  5   52  52  absorption food     52  data_name_x4    2
...

But what I want is to get:

id  group_id    keyword_id  id  keyword id  data_name_x1    data_name_x2    data_name_x3    data_name_x4
12  5   52  52  absorption food     52      6               8               26              2
...

So I can sort and use pagination for the datagrids easly, otherwise I have no idea how to do it, because when using big data sets I can’t just dump everything into an array, too much data.

This is the schema:

-- --------------------------------------------------------
-- Table structure for table `keywords`

CREATE TABLE IF NOT EXISTS `keywords` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `keyword` varchar(255) NOT NULL,
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `keyword` (`keyword`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=115386 ;


-- --------------------------------------------------------
-- Table structure for table `data`

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(10) unsigned NOT NULL,
  `name` varchar(100) NOT NULL,
  `value` varchar(15) NOT NULL,
  UNIQUE KEY `id` (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


-- --------------------------------------------------------
-- Table structure for table `projects`
--

CREATE TABLE IF NOT EXISTS `projects` (
  `id` int(10) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `parent` varchar(100) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;


-- --------------------------------------------------------
-- Table structure for table `group_keywords`

CREATE TABLE IF NOT EXISTS `group_keywords` (
  `id` int(10) NOT NULL auto_increment,
  `group_id` int(10) NOT NULL,
  `keyword_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `group_id` (`group_id`,`keyword_id`),
  KEY `keyword_id` (`keyword_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=119503 ;


-- --------------------------------------------------------
-- Constraints for table `data`
--
ALTER TABLE `data`
  ADD CONSTRAINT `data_ibfk_1` FOREIGN KEY (`id`) REFERENCES `keywords` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- --------------------------------------------------------
-- Constraints for table `group_keywords`
--
ALTER TABLE `group_keywords`
  ADD CONSTRAINT `group_keywords_ibfk_1` FOREIGN KEY (`keyword_id`) REFERENCES `keywords` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
  • 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-16T22:28:22+00:00Added an answer on May 16, 2026 at 10:28 pm

    This operation is traditionally called “unpivot” and a few RDBMSs support it, but MySQL doesn’t appear to be one of them. You have two options, do it in SQL or do it in PHP. In MySQL it looks something like this with self-joins (I don’t know which field qualifies as an ID field for you, so forgive me creating my own example). From a performance perspective, make sure you index both the ID and Column Name, otherwise these joins will crawl.

    shapes
    ID  Name   Value
    1   Color  Red
    1   Shape  Circle
    ... for more "columns"
    2   Color  Green
    2   Shape  Square
    ... for more "columns"
    
    SELECT
      A.ID,
      B.Value as Color,
      C.Value as Shape
      ... for more "columns"
    FROM shapes A
    LEFT JOIN shapes B ON B.ID = A.ID AND B.Name = 'Color'
    LEFT JOIN shapes C ON C.ID = A.ID AND C.Name = 'Shape'
    ... for more "columns"
    

    Which should net us (unless my head-SQL-parser is misrunning tonight):

    ID  Color   Shape
    1   Red     Circle
    2   Green   Square
    

    For the PHP version, you don’t necessarily have to load up an array, you can stream it. Sort by the PK and walk down it setting the properties. In pseudocode:

    Set X to undefined
    Get a Record
       Check the ID property, if it's different than X, create a new object, set X to the new ID, and yield the previous object
       Set the property of the object based on the "Name" column of our result
    

    Hope this helps!

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

Sidebar

Related Questions

I have a core data model, using the database store, that contains entities with
I have to store messages that my web app fetch from Twitter into a
I have two database fields $object->value and $object->max_value and want to store the proportion
I am making a synonyms list that I will store it in database and
I am using django-hitcount to tally hits to my database objects. I want to
i am using implode to store my image paths. with the special symbol '~'
How to get all name tables, name columns, and values in database. Example i
Sorry for this trivial question, but I am new to programming and I wanted
I want to create an application using php and mysql. Assume i am making
I'm exploring different ways of storing user-uploaded files (all are MS Office documents or

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.