MySQL 查询以根据自定义列名和值获取值

Posted

技术标签:

【中文标题】MySQL 查询以根据自定义列名和值获取值【英文标题】:MySQL query to get values based on custom column name and values 【发布时间】:2021-07-24 06:59:11 【问题描述】:

我有两张这样的桌子

    wp_posts

  ID   post_date      post_title   post_type   post_status
  1    2020-12-10      ABC         institute   publish
  2    2020-12-16      DEG         institute   publish
  3    2020-12-11      XXY         institute   publish
  4    2020-12-12      ABX         institute   publish
  5    2020-12-24      ZYU         institute   publish
  6    2020-12-28      ANM         institute   publish
  7    2020-12-16      DDK         institute   publish
  8    2020-12-30      LKI         institute   publish
  9    2020-12-30      LKI         institute   publish
  10   2020-12-31      LKI         institute   publish
  11   2020-12-16      TGY         institute   publish
  12   2020-12-16      MUN         institute   publish

 wp_postmeta

meta_id  post_id   meta_key         meta_value
1          1       country_id           10
2          1       registartion_by      online
3          3       registartion_by      offline
4          4       country_id           100
5          3       country_id           100
6          4       registartion_by      online
7          5       registartion_by      online
8          7       registartion_by      offline
9          7       country_id           101
10         5       country_id           102
11         8       country_id           103
12         9       registartion_by      online
13         8       registartion_by      offline
14         9       country_id           10
15         10      country_id           104
16         10      registartion_by      offline
17         11      country_id           101
18         11      registartion_by      online

现在我想进行一些查询,以便最终输出显示为这样的内容

Date            Country   Offline_registration  Online_registartion
2020-12-10      10          0                      1
2020-12-11      100         1                      0
2020-12-12      100         0                      1
2020-12-16      101         1                      1
2020-12-24      102         0                      1
2020-12-30      103         0                      1
2020-12-31      104         1                      0

为了实现这一点,我做了一些这样的查询

SELECT date( posts.post_date ) as Date , postmeta.meta_value as Country
  FROM wp_posts as posts INNER JOIN wp_postmeta as postmeta on posts.ID = postmeta.post_id WHERE posts.post_type = 'institute' AND posts.post_status = 'publish' AND postmeta.meta_key = 'country_id' GROUP BY postmeta.meta_value

但我无法获取注册类型计数的数据,即(在线/离线注册)。那么有人可以告诉我如何实现吗?

任何建议或意见都会非常可观。 谢谢。

【问题讨论】:

【参考方案1】:

聚合两次

select p.post_date,  m.country_id,
    sum(case when registartion_by='offline' then 1 end) Offline_registartion,
    sum(case when registartion_by='online' then 1 end) Online_registartion
from (
   select post_id,
      max(case when meta_key ='country_id' then meta_value end) country_id, 
      max(case when meta_key ='registartion_by' then meta_value end) registartion_by,
   from wp_postmeta 
   group by post_id ) m
join  wp_posts p on p.ID = m.post_id
group by post_date

mysqlsum(case when registartion_by='offline' then 1 end)可以简写为sum(registartion_by='offline')

【讨论】:

【参考方案2】:

我不知道你为什么从结果中排除了| 2020-12-30 | 10 | 0 | 1 |,所以我假设你正在寻找这样的东西:

DROP TABLE IF EXISTS wp_posts;

CREATE TABLE wp_posts
(id INT AUTO_INCREMENT PRIMARY KEY
,post_date DATE NOT NULL
,post_title  CHAR(3) NOT NULL
);

INSERT INTO wp_posts VALUES
( 1,'2020-12-10','ABC'),
( 2,'2020-12-16','DEG'),
( 3,'2020-12-11','XXY'),
( 4,'2020-12-12','ABX'),
( 5,'2020-12-24','ZYU'),
( 6,'2020-12-28','ANM'),
( 7,'2020-12-16','DDK'),
( 8,'2020-12-30','LKI'),
( 9,'2020-12-30','LKI'),
(10,'2020-12-31','LKI'),
(11,'2020-12-16','TGY'),
(12,'2020-12-16','MUN'); 

DROP TABLE IF EXISTS wp_postmeta;

CREATE TABLE wp_postmeta
(meta_id INT AUTO_INCREMENT PRIMARY KEY
,post_id INT NOT NULL
,meta_key VARCHAR(20) NOT NULL
,meta_value VARCHAR(20) NOT NULL
,UNIQUE(post_id,meta_key)
);

INSERT INTO wp_postmeta VALUES
( 1, 1,'country_id',10),
( 2, 1,'registration_by','online'),
( 3, 3,'registration_by','offline'),
( 4, 4,'country_id','100'),
( 5, 3,'country_id','100'),
( 6, 4,'registration_by','online'),
( 7, 5,'registration_by','online'),
( 8, 7,'registration_by','offline'),
( 9, 7,'country_id','101'),
(10, 5,'country_id','102'),
(11, 8,'country_id','103'),
(12, 9,'registration_by','online'),
(13, 8,'registration_by','offline'),
(14, 9,'country_id','10'),
(15,10,'country_id','104'),
(16,10,'registration_by','offline'),
(17,11,'country_id','101'),
(18,11,'registration_by','online');

...

SELECT p.post_date
     , MAX(CASE WHEN m.meta_key = 'country_id' THEN m.meta_value END) country 
     , MAX(CASE WHEN m.meta_key = 'registration_by' THEN CASE WHEN m.meta_value = 'offline' THEN 1 ELSE 0 END END) offline
     , MAX(CASE WHEN m.meta_key = 'registration_by' THEN CASE WHEN m.meta_value = 'online' THEN 1 ELSE 0 END END) online
  FROM wp_posts p 
  JOIN wp_postmeta m 
    ON m.post_id = p.id
 GROUP
    BY p.id
     , p.post_date
 ORDER
    BY country
     , post_date;
     
+------------+---------+---------+--------+
| post_date  | country | offline | online |
+------------+---------+---------+--------+
| 2020-12-10 | 10      |       0 |      1 |
| 2020-12-30 | 10      |       0 |      1 |
| 2020-12-11 | 100     |       1 |      0 |
| 2020-12-12 | 100     |       0 |      1 |
| 2020-12-16 | 101     |       0 |      1 |
| 2020-12-16 | 101     |       1 |      0 |
| 2020-12-24 | 102     |       0 |      1 |
| 2020-12-30 | 103     |       1 |      0 |
| 2020-12-31 | 104     |       1 |      0 |
+------------+---------+---------+--------+

【讨论】:

【参考方案3】:

您可以通过两次加入wp_postmeta 来做到这一点,一次用于国家/地区,一次用于注册:

SELECT date(p.post_date) as Date, pmc.meta_value as Country,
       SUM( pmr.meta_value = 'online' ) as num_online,
       SUM( pmr.meta_value = 'offline' ) as num_offline
 FROM wp_posts p INNER JOIN
      wp_postmeta pmc 
      ON pmc.post_id = p.id AND
         pmc.meta_key = 'country_id' LEFT JOIN
      wp_postmeta pmr
      ON pmc.post_id = p.id AND
         pmc.meta_key = 'registion_by' 
WHERE p.post_type = 'institute' AND
      p.post_status = 'publish'
GROUP BY date, country;

【讨论】:

以上是关于MySQL 查询以根据自定义列名和值获取值的主要内容,如果未能解决你的问题,请参考以下文章

如何根据预先确定的约束值自定义热图颜色?

R_Studio(癌症)以等宽类别值自定义类别值等频类别值(分为5类)

在python mysql查询中动态地传递列名和值。

MySQL查询以获取列名?

Combobox值自定义(不通过数据库)

属性上的自定义属性 - 获取属性属性的类型和值