SELECT multiple values from a single column in Wordpress Database

southafricanrob

I am trying to output the post details, as well as some custom fields in wordpress's database. I have custom fields called 'thumbnail' and 'video'

SELECT ID, post_title, post_content, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.id=wp_postmeta.post_id
WHERE wp_postmeta.meta_key = "thumnail"
OR wp_postmeta.meta_key = "video"

So far I am getting individual rows like this:

| ID  | Title     | Content     | Custom Fields                      |
|--------------------------------------------------------------------|
| 234 | INCEPTION | Content etc | wp-content/thumbnail/inception.jpg |
| 234 | INCEPTION | Content etc | wp-content/video/inception.flv     |

but what i would like is (ie one line per post)

|ID |Title     |Content     |Thumbnail                  |Video                      |
|-----------------------------------------------------------------------------------|
|234|INCEPTION |Content etc |wp-content/..inception.jpg |wp-content/..inception.flv |

Could someone advise on how to separate the different values into columns within the SELECT statement?

Danijel

This process is called "pivot table" or "crosstab report". There is no PIVOT command in MySQL but it can be done manually, here are two examples. The first example uses IF statement, the second one CASE statement. And an aggregate function is used to get rid of the duplicate rows.

SELECT ID, post_title, post_content,
MAX( IF ( wp_postmeta.meta_key = "thumnail", wp_postmeta.meta_value, '' ) ) AS Thumbnail,
MAX( IF ( wp_postmeta.meta_key = "video", wp_postmeta.meta_value, '' ) ) AS Video
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.id=wp_postmeta.post_id
WHERE wp_postmeta.meta_key IN ( "thumnail", "video" )
GROUP BY ID

SELECT ID, post_title, post_content,
MAX( CASE WHEN wp_postmeta.meta_key = "thumnail" THEN wp_postmeta.meta_value ELSE '' END ) AS Thumbnail,
MAX( CASE WHEN wp_postmeta.meta_key = "video" THEN wp_postmeta.meta_value ELSE '' END ) AS Video
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.id=wp_postmeta.post_id
WHERE wp_postmeta.meta_key IN( "thumnail", "video" )
GROUP BY ID

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select values from multiple columns into single column

From Dev

Select values from multiple columns into single column

From Dev

Select statement with multiple rows from condition on values in single column

From Dev

Select statement with multiple rows from condition on values in single column

From Dev

How to SELECT multiple values from a row and join it as a single column value

From Dev

Select multiple column values in single query - oracle

From Dev

Select comma separated values from single column

From Dev

Using read.csv.sql to select multiple values from a single column

From Dev

inserting multiple checkbox values into a single column - SQL database

From Dev

how to insert multiple checkbox values in single column in database using php

From Dev

Is it possible to select only repeated values from a single column using eloquent?

From Dev

select limited duplicate values from a specific column for multiple values

From Dev

LINQ conditional select from table and return multiple column as a single list

From Dev

R - Select IDs that meet multiple criteria from single column

From Dev

how to select multiple values from a single level of a dataframe multiindex

From Dev

select multiple column into a single row

From Dev

Select multiple selected values from a column from each row

From Dev

Select Number of Values in Database Column

From Dev

how to run multiple website with single database in wordpress

From Dev

Saving "Select Multiple" values in a database

From Dev

Select rows from a DataFrame based on multiple values in a column in pandas

From Dev

MySql: select values from one column into multiple columns in the result

From Dev

MySQL: Select multiple max values from one column

From Dev

Pivot table multiple column values in a single column

From Dev

Select immediate smaller/larger values when comparing multiple columns to another single column

From Dev

Multiple database row values in single variable

From Dev

Select unique values from Database

From Dev

Multiple values set on select2 in WordPress

From Dev

Multiple tables in select statement to get single column

Related Related

  1. 1

    Select values from multiple columns into single column

  2. 2

    Select values from multiple columns into single column

  3. 3

    Select statement with multiple rows from condition on values in single column

  4. 4

    Select statement with multiple rows from condition on values in single column

  5. 5

    How to SELECT multiple values from a row and join it as a single column value

  6. 6

    Select multiple column values in single query - oracle

  7. 7

    Select comma separated values from single column

  8. 8

    Using read.csv.sql to select multiple values from a single column

  9. 9

    inserting multiple checkbox values into a single column - SQL database

  10. 10

    how to insert multiple checkbox values in single column in database using php

  11. 11

    Is it possible to select only repeated values from a single column using eloquent?

  12. 12

    select limited duplicate values from a specific column for multiple values

  13. 13

    LINQ conditional select from table and return multiple column as a single list

  14. 14

    R - Select IDs that meet multiple criteria from single column

  15. 15

    how to select multiple values from a single level of a dataframe multiindex

  16. 16

    select multiple column into a single row

  17. 17

    Select multiple selected values from a column from each row

  18. 18

    Select Number of Values in Database Column

  19. 19

    how to run multiple website with single database in wordpress

  20. 20

    Saving "Select Multiple" values in a database

  21. 21

    Select rows from a DataFrame based on multiple values in a column in pandas

  22. 22

    MySql: select values from one column into multiple columns in the result

  23. 23

    MySQL: Select multiple max values from one column

  24. 24

    Pivot table multiple column values in a single column

  25. 25

    Select immediate smaller/larger values when comparing multiple columns to another single column

  26. 26

    Multiple database row values in single variable

  27. 27

    Select unique values from Database

  28. 28

    Multiple values set on select2 in WordPress

  29. 29

    Multiple tables in select statement to get single column

HotTag

Archive