如何使用SQL基于角色检索用户数据?

时间:2017-05-05 作者:Arun Yogesh

我需要从SQL数据库中检索用户数据,我已使用下面给出的SQL代码成功地做到了这一点:

SELECT 
    wpu6_users.display_name AS n_user_display_name,
    t1.meta_value AS n_user_first_name,
    t2.meta_value AS n_user_phone, 
    wpu6_users.user_email AS n_user_user_email,
    t3.meta_value AS n_user_address_1,
    t4.meta_value AS n_user_address_2,
    t5.meta_value AS n_user_area,
    t6.meta_value AS n_user_city,
    t7.meta_value AS n_user_state,
    t8.meta_value AS n_user_country,
    t9.meta_value AS n_user_postcode
    FROM wpu6_users
    LEFT JOIN wpu6_usermeta AS t1 ON t1.user_id = wpu6_users.ID 
    LEFT JOIN wpu6_usermeta AS t2 ON t1.user_id = t2.user_id 
    LEFT JOIN wpu6_usermeta AS t3 ON t2.user_id = t3.user_id  
    LEFT JOIN wpu6_usermeta AS t4 ON t3.user_id = t4.user_id
    LEFT JOIN wpu6_usermeta AS t5 ON t4.user_id = t5.user_id 
    LEFT JOIN wpu6_usermeta AS t6 ON t5.user_id = t6.user_id  
    LEFT JOIN wpu6_usermeta AS t7 ON t6.user_id = t7.user_id
    LEFT JOIN wpu6_usermeta AS t8 ON t7.user_id = t8.user_id
    LEFT JOIN wpu6_usermeta AS t9 ON t8.user_id = t9.user_id
    WHERE t1.meta_key = \'billing_first_name\' 
    AND t2.meta_key = \'billing_phone\' 
    AND t3.meta_key = \'billing_address_1\' 
    AND t4.meta_key = \'billing_address_2\'
    AND t5.meta_key = \'mv_area\' 
    AND t6.meta_key = \'billing_city\' 
    AND t7.meta_key = \'billing_state\'
    AND t8.meta_key = \'billing_country\' 
    AND t9.meta_key = \'billing_postcode\' 
现在,我正在尝试使用SQL根据角色显示用户。有人能建议我如何取回它吗?

提前谢谢。

1 个回复
SO网友:JItendra Rana

在中添加以下内容WHERE SQL查询的条件。

WHERE t1.meta_key = "<?php echo $wpdb->prefix; ?>capabilities" 
AND ( t1.meta_value LIKE \'%ROLE_YOU_WANT_TO_FILTER%\')
让我知道它是否适合你。

相关推荐

高级WP查询占用了SQL服务器

我最近为一位房地产经纪人的客户开发了一个网站。项目中包含一个名为listing. 此帖子类型用于他们的所有列表,其他信息存储在postmeta 表通过使用Advanced Custom Fields Pro.在网站的前端,我开发了一个过滤栏,用户可以通过几个过滤器进行选择并提交表单,然后生成一个复杂的WP\\u查询以返回匹配的列表。很简单!虽然大多数搜索都工作得很好,但一些更复杂的搜索将生成占用线程的SQL查询。当数据库需要刷新堆积的所有内容时,服务器基本上会冻结,直到有人终止进程。这在所有环境中都会发生