简体   繁体   English

同一张桌子上有多个选择

[英]Multiple selects on the same table

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: 我正在处理wordpress数据库,并且有这些SQL查询可以自己运行:

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. 编辑:我正在查询wp_postmeta表,它具有4列meta_id,post_id,meta_key和meta_value列。 I'm selecting the meta_value from a custom field I've added (called myfield1, saved in the meta_key column). 我从添加的自定义字段(称为myfield1,保存在meta_key列中)中选择meta_value。 I'm ordering by post_id which is the ID of the posts in wordpress so I get chronologic order. 我按post_id排序,post_id是wordpress中帖子的ID,因此我按时间顺序排序。

But I have 5 custom fields, myfield1 to myfield5. 但是我有5个自定义字段,从myfield1到myfield5。 And I wanted to query all of them once. 我想对所有这些查询一次。

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;

You can't select the same column with as different alias (`'meta_value' AS 'Name' ... etc) and get the results as one set. 您不能选择别名不同的同一列(“ meta_value” AS“名称” ...等)并将结果作为一组来获取。 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. 哪一个都将在meta_key列中为您提供所有值在一个结果集中,因此您可以分辨出哪个是哪个。

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! 我尚未检查SQL语法,因此,如果出现语法错误,请原谅!

Assuming this is mySQL? 假设这是mySQL?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM