从Wordpress获取user_meta的纯SQL的说明

时间:2011-09-25 02:45:44

标签: mysql sql wordpress

我有以下SQL命令,可以在Wordpress中获取user_meta详细信息。这可以很好地将数据放在一个表中,每个用户信息一行,每列中每个用户的信息,但我只是想知道它是如何工作的。任何人都可以在下面解释一下吗?:

SELECT
    u.id, u.user_login,
    MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END) AS title,
    MIN(CASE m.meta_key WHEN 'first_name' THEN m.meta_value END) AS first_name,
    MIN(CASE m.meta_key WHEN 'last_name' THEN m.meta_value END) AS last_name,
    MIN(CASE m.meta_key WHEN 'suburb' THEN m.meta_value END) AS phone,
    MIN(CASE m.meta_key WHEN 'state' THEN m.meta_value END) AS state,
    MIN(CASE m.meta_key WHEN 'country' THEN m.meta_value END) AS country,
    MIN(CASE m.meta_key WHEN 'postcode' THEN m.meta_value END) AS postcode,
    MIN(CASE m.meta_key WHEN 'contact_no' THEN m.meta_value END) AS contact_no,
    MIN(CASE m.meta_key WHEN 'email' THEN m.meta_value END) AS email,
    MIN(CASE m.meta_key WHEN 'occupation' THEN m.meta_value END) AS occupation,
    MIN(CASE m.meta_key WHEN 'workplace' THEN m.meta_value END) AS workplace,
    MIN(CASE m.meta_key WHEN 'maternitybg' THEN m.meta_value END) AS maternitybg,
    MIN(CASE m.meta_key WHEN 'trainingdate' THEN m.meta_value END) AS trainingdate,
    MIN(CASE m.meta_key WHEN 'traininglocation' THEN m.meta_value END) AS traininglocation,
    MIN(CASE m.meta_key WHEN 'coltraining' THEN m.meta_value END) AS coltraining,
    MIN(CASE m.meta_key WHEN 'trainingyear' THEN m.meta_value END) AS trainingyear,
    MIN(CASE m.meta_key WHEN 'coltraining' THEN m.meta_value END) AS coltraining,
    MIN(CASE m.meta_key WHEN 'isinstructor' THEN m.meta_value END) AS isinstructor,
    MIN(CASE m.meta_key WHEN 'gender' THEN m.meta_value END) AS gender,
    MIN(CASE m.meta_key WHEN 'idf_indig_tsi' THEN m.meta_value END) AS idf_indig_tsi,
    MIN(CASE m.meta_key WHEN 'idf_ct_ld' THEN m.meta_value END) AS idf_ct_ld,
    MIN(CASE m.meta_key WHEN 'comments' THEN m.meta_value END) AS comments

    FROM wp_users u
      LEFT JOIN wp_usermeta m ON u.ID = m.user_id
        AND m.meta_key IN ('title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments')
    GROUP BY u.ID

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

不确定你想要解释什么部分,但无论如何,我们走了

这部分是关于指定所需的列:

SELECT
    u.id, u.user_login,

后面是包含源表的部分:

FROM wp_users u
  LEFT JOIN wp_usermeta m ON u.ID = m.user_id

使用left join,即使wp_users中没有匹配项,也要确保从wp_usermeta获取行。 ON u.ID = m.user_idwp_userswp_usermeta之间的行连接起来。

显然,wp_usermeta是键值表,这限制了您感兴趣的键。

AND m.meta_key IN ('title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments')

最后,group by子句将为u.ID的每个不同值输出一行。

GROUP BY u.ID

使用案例陈述,字段列表中还有一个小技巧。

MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END) AS title,

如果m.meta_value等于m.meta_key,则首先获得值'title',否则值将为null

CASE m.meta_key WHEN 'title' THEN m.meta_value END

如果给定用户'title'中有多个wp_usermeta个键,您将获得min()值。标题'公交车司机'将在标题'出租车司机'之前被选中。

MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END)

最后,您会在列中检索列别名title

MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END) AS title,