Appending key : value data onto MySQL table

Richard Denton

Excuse my ignorance, but is there a tidy way to automatically join or append meta values (set as temporary columns by key) from one table to another?

Table items
ID (int) title (varchar 255)
content (longtext)

Table meta
ID (int)
item_id (int)
key (varchar 255)
value (longtext)

Ideally, I'd like to be able to do a generic query like

SELECT * FROM items

and have the output as

item.ID item.title item.content metakey1 metakey2 1 Listing Title Listing content meta_val1 meta_val2 2 Another Title Listing content meta_val1 meta_val2

zedfoxus

You can store your data in XML format and use MySQL XML Functions to extract them.

Example:

create table items (id int, title varchar(100), content longtext);
create table meta (id int, item_id int, metadata varchar(100));
insert into items values (1, 'test item1', 'some content');
insert into meta values (1, 1, '<key1>value1</key1><key2>value2</key2>');

select 
  i.id, i.title, i.content, 
  extractvalue(m.metadata, '//key1[$1]') as key1, 
  extractvalue(m.metadata, '//key2[$1]') as key2 
from items i 
inner join meta m on i.id = m.item_id;

+------+------------+--------------+--------+--------+
| id   | title      | content      | key1   | key2   |
+------+------------+--------------+--------+--------+
|    1 | test item1 | some content | value1 | value2 |
+------+------------+--------------+--------+--------+

You can create a view called items_with_metadata and then call select * from items_with_metadata to get the kind of output you desire.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Appending a value to a key in Python?

From Dev

appending key value to a list from a data frame in R

From Dev

Condition for appending data into table

From Dev

Appending a user selected value onto a preset string in ComboBox

From Dev

Scrapy appending Key: value pairs to another Key

From Dev

Scrapy appending Key: value pairs to another Key

From Dev

appending onto JSON in python

From Dev

Ajax appending data to existing table

From Dev

Error trying to insert key value into a MySQL table

From Dev

Passing a value from input in table onto controller

From Dev

html form into mysql database then onto a table

From Dev

Inserts into MySQL table going onto separate rows

From Dev

Appending Pandas dataframe to sqlite table by primary key

From Dev

Appending Pandas dataframe to sqlite table by primary key

From Dev

How to copy table's data to another duplicate table with incremented primary key and some of the column with incremented value. in mySQL

From Dev

Fetching Data from PLIST onto table 3

From Dev

Fetching Data from PLIST onto table 3

From Dev

insert JsonArray key value data into mysql database

From Dev

Python 2.7: Appending to a list value of a dictionary key

From Dev

Appending to dict of lists adds value to every key

From Dev

Appending Key/Value to object inside of Promise

From Dev

Excel to SQL table field value appending with 0

From Dev

Jquery replace table data instead of appending

From Dev

Knockout data table appending values (not reloading)

From Dev

For loop for turning pages and appending data from the table

From Dev

mySQL: selecting based on several keys in key/value table

From Dev

MySQL table with key/value pairs, get keys as column names

From Dev

Construct MySQL query ( meta_key/meta_value table)

From Dev

Construct MySQL query ( meta_key/meta_value table)

Related Related

  1. 1

    Appending a value to a key in Python?

  2. 2

    appending key value to a list from a data frame in R

  3. 3

    Condition for appending data into table

  4. 4

    Appending a user selected value onto a preset string in ComboBox

  5. 5

    Scrapy appending Key: value pairs to another Key

  6. 6

    Scrapy appending Key: value pairs to another Key

  7. 7

    appending onto JSON in python

  8. 8

    Ajax appending data to existing table

  9. 9

    Error trying to insert key value into a MySQL table

  10. 10

    Passing a value from input in table onto controller

  11. 11

    html form into mysql database then onto a table

  12. 12

    Inserts into MySQL table going onto separate rows

  13. 13

    Appending Pandas dataframe to sqlite table by primary key

  14. 14

    Appending Pandas dataframe to sqlite table by primary key

  15. 15

    How to copy table's data to another duplicate table with incremented primary key and some of the column with incremented value. in mySQL

  16. 16

    Fetching Data from PLIST onto table 3

  17. 17

    Fetching Data from PLIST onto table 3

  18. 18

    insert JsonArray key value data into mysql database

  19. 19

    Python 2.7: Appending to a list value of a dictionary key

  20. 20

    Appending to dict of lists adds value to every key

  21. 21

    Appending Key/Value to object inside of Promise

  22. 22

    Excel to SQL table field value appending with 0

  23. 23

    Jquery replace table data instead of appending

  24. 24

    Knockout data table appending values (not reloading)

  25. 25

    For loop for turning pages and appending data from the table

  26. 26

    mySQL: selecting based on several keys in key/value table

  27. 27

    MySQL table with key/value pairs, get keys as column names

  28. 28

    Construct MySQL query ( meta_key/meta_value table)

  29. 29

    Construct MySQL query ( meta_key/meta_value table)

HotTag

Archive