Multiple selects on the same table

2019-09-16 03:01发布

I'm trying to figure it ou a way to make this selects in the same query. I'm working on the wordpress database, and I have these SQL queries that works by themselves:

SELECT `meta_value` AS 'Name' FROM `wp_postmeta` WHERE `meta_key` =     "myfield1" ORDER BY `post_id` ASC
SELECT `meta_value` AS 'Department' FROM `wp_postmeta` WHERE `meta_key` = "myfield2" ORDER BY `post_id` ASC
SELECT `meta_value` AS 'Location' FROM `wp_postmeta` WHERE `meta_key` = "myfield3" ORDER BY `post_id` ASC
SELECT `meta_value` AS 'Job Title' FROM `wp_postmeta` WHERE `meta_key` = "myfield4" ORDER BY `post_id` ASC
SELECT `meta_value` AS 'Social Number' FROM `wp_postmeta` WHERE `meta_key` = "myfield5" ORDER BY `post_id` ASC

But I'd like to have them all on the same query so I can export the data later on a report. Here's what I've tried:

SELECT  
    (SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = "myfield1" ORDER BY `post_id` ASC) As name,
    (SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = "myfield2" ORDER BY `post_id` ASC) AS department,
    (SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = "myfield3" ORDER BY `post_id` ASC) AS location,
    (SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = "myfield4" ORDER BY `post_id` ASC) AS jobtitle,
    (SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = "myfield5" ORDER BY `post_id` ASC) AS socnumber

But that's returning commands out of sync. Anyone knows how to fix this?

#2014 - Commands out of sync; you can't run this command now 

Edit: I'm querying the wp_postmeta table, it has 4 columns meta_id, post_id, meta_key and meta_value columns. I'm selecting the meta_value from a custom field I've added (called myfield1, saved in the meta_key column). I'm ordering by post_id which is the ID of the posts in wordpress so I get chronologic order.

But I have 5 custom fields, myfield1 to myfield5. And I wanted to query all of them once.

3条回答
Fickle 薄情
2楼-- · 2019-09-16 03:21

To get a resultset with five columns (one for each custom field) you have to join the table to itself five times...

Something like this:

SELECT
    pm1.meta_value AS 'Name' 
    pm2.meta_value AS 'Department'
    pm3.meta_value AS 'Location'
    pm4.meta_value AS 'Job Title'
    pm5.meta_value AS 'Social Number'

 FROM `wp_postmeta` pm1
    JOIN `wp_postmeta` pm2 ON pm1.post_id = pm2.post_id
    JOIN `wp_postmeta` pm3 ON pm1.post_id = pm3.post_id
    JOIN `wp_postmeta` pm4 ON pm1.post_id = pm4.post_id
    JOIN `wp_postmeta` pm5 ON pm1.post_id = pm5.post_id

WHERE
    pm1.meta_key = "myfield1"
    AND pm2.meta_key = "myfield2"
    AND pm3.meta_key = "myfield3"
    AND pm4.meta_key = "myfield4"
    AND pm5.meta_key = "myfield5"

 ORDER BY `post_id` ASC

I haven't checked the SQL syntax, so forgive me if there's a syntax error!

Assuming this is mySQL?

查看更多
姐就是有狂的资本
3楼-- · 2019-09-16 03:37

One method is to use conditional aggregation:

SELECT MAX(CASE WHEN `meta_key` = 'myfield1' THEN meta_value END) as Name,
       MAX(CASE WHEN `meta_key` = 'myfield2' THEN meta_value END) as Department,
       MAX(CASE WHEN `meta_key` = 'myfield3' THEN meta_value END) as Location,
       MAX(CASE WHEN `meta_key` = 'myfield4' THEN meta_value END) as JobTitle,
       MAX(CASE WHEN `meta_key` = 'myfield5' THEN meta_value END) as SocialNumber
FROM `wp_postmeta`
WHERE `meta_key` = "myfield5" 
GROPU BY post_id
ORDER B post_id ASC;
查看更多
走好不送
4楼-- · 2019-09-16 03:40

You can't select the same column with as different alias (`'meta_value' AS 'Name' ... etc) and get the results as one set. The column can only have one name!

The best you can do is something like this:

SELECT `meta_value`, `meta_key`
FROM `wp_postmeta`
WHERE `meta_key` IN ("myfield1", "myfield2", "myfield3", "myfield4", "myfield5")
ORDER BY `post_id` ASC

Which will give you all your values in one result set, with the meta_key column too so you can tell which is which.

查看更多
登录 后发表回答