查询从元数据表中提取用户数据

时间:2016-07-18 18:48:07

标签: mysql sql

我无法弄清楚这个查询有什么问题。我有一个wordpress网站,我试图从wp_users和wp_usermeta中提取用户数据。这是我目前的查询。它只会吸引一定数量的用户而不是全部。

SELECT wp_users.id AS ID
    ,metafn.meta_value AS "First Name"
    ,metaln.meta_value AS "Last Name"
    ,wp_users.user_email AS "Email"
    ,metap.meta_value AS "Phone"
    ,metaad.meta_value AS "Address"
    ,metac.meta_value AS "City"
    ,metas.meta_value AS "State"
    ,metapc.meta_value AS "Zip"
    ,metad.meta_value AS "Money Donated"
FROM wp_users
INNER JOIN wp_usermeta metafn ON (
        wp_users.ID = metafn.user_id
        AND metafn.meta_key = 'first_name'
        )
INNER JOIN wp_usermeta metaln ON (
        wp_users.ID = metaln.user_id
        AND metaln.meta_key = 'last_name'
        )
INNER JOIN wp_usermeta metap ON (
        wp_users.ID = metap.user_id
        AND metap.meta_key = 'billing_phone'
        )
INNER JOIN wp_usermeta metaad ON (
        wp_users.ID = metaad.user_id
        AND metaad.meta_key = 'billing_address_1'
        )
INNER JOIN wp_usermeta metac ON (
        wp_users.ID = metac.user_id
        AND metac.meta_key = 'billing_city'
        )
INNER JOIN wp_usermeta metas ON (
        wp_users.ID = metas.user_id
        AND metas.meta_key = 'billing_state'
        )
INNER JOIN wp_usermeta metapc ON (
        wp_users.ID = metapc.user_id
        AND metapc.meta_key = 'billing_postcode'
        )
INNER JOIN wp_usermeta metad ON (
        wp_users.ID = metad.user_id
        AND metad.meta_key = '_money_spent'
        )
WHERE NOT EXISTS (
        SELECT *
        FROM donors
        WHERE donors.user_id = wp_users.id
        )

抱歉格式化。任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:0)

我发现我需要将join语句更改为LEFT JOIN而不是INNER。我没有意识到我的数据中有一些空值在使用INNER JOIN时没有出现。谢谢!