我正在创建一个SQL查询,其中要显示从woocommerce商店购买了可下载产品的客户的全名和电子邮件.我正在寻找这样的东西:user_id first_name last_name user_email order--------------------------------...
我正在创建一个SQL查询,其中要显示从woocommerce商店购买了可下载产品的客户的全名和电子邮件.我正在寻找这样的东西:
user_id first_name last_name user_email order
---------------------------------------------------------
1 Peter Jones a12@gmail Doc_1
在上表中,Doc_1代表产品的名称,在这种情况下,user_id = 1的客户可以下载Doc_1.对于wordpress用户,您知道表wp_usermeta包含所有用户信息,如下所示:
umeta_id user_id meta_key meta_value
---------------------------------------------
1 1 nickname petjon123
2 1 first_name Peter
3 1 last_name Jones
... ... ... ...
该列表下降了,包含更多的meta_value [s],包括电子邮件等.如果meta_key [s]是表标头,彼此显示first_name和last_name而不是放在顶部,则一切都会变得更加容易.到目前为止,我的查询中显示的是first_name,但是我在获取last_name时遇到了麻烦.我的SQL查询如下所示:
SELECT
wp_usermeta.user_id,
wp_usermeta.meta_value AS 'first_name',
Wp_woocommerce_downloadable_product_permissions_1.user_email AS 'email',
Wp_woocommerce_order_items_1.order_item_name AS 'order'
FROM Wp_woocommerce_order_items
INNER JOIN Wp_woocommerce_downloadable_product_permissions
ON Wp_woocommerce_order_items.order_id = Wp_woocommerce_downloadable_product_permissions.order_id,
(Wp_woocommerce_order_items AS Wp_woocommerce_order_items_1
INNER JOIN Wp_woocommerce_downloadable_product_permissions AS Wp_woocommerce_downloadable_product_permissions_1
ON Wp_woocommerce_order_items_1.order_id = Wp_woocommerce_downloadable_product_permissions_1.order_id)
INNER JOIN wp_usermeta
ON Wp_woocommerce_downloadable_product_permissions_1.user_id = wp_usermeta.user_id
GROUP BY wp_usermeta.user_id,
wp_usermeta.meta_value,
Wp_woocommerce_downloadable_product_permissions_1.user_email,
Wp_woocommerce_order_items_1.order_item_name,
Wp_woocommerce_order_items_1.order_item_type,
wp_usermeta.meta_key
HAVING (((Wp_woocommerce_order_items_1.order_item_type) = 'line_item')
AND ((wp_usermeta.meta_key) = 'first_name'));
给我这个结果:
user_id first_name user_email order
--------------------------------------------
1 Peter a12@gmail Doc_1
我手动检查了这些值,它们是正确的,但是,我必须说,如果不对结果进行分组,则会得到1000倍的结果,因此对数据进行分组可以通过基本上删除明显过多的重复项来简化结果.我怀疑该查询是否有效,因为直到结果出现为止的执行时间是/-20368.14 ms,因此对于改进此代码的任何建议都非常欢迎.如此说来,我以为我必须做另一个查询才能获得last_name,从而使它成为一个嵌套查询.如果我在MS Access中打开表,请创建一个查询Query1(上面的查询),然后将Query1与该表连接起来,以获得所需的结果.用于此的SQL代码如下:
SELECT
Query1.user_id,
Query1.first_name,
wp_usermeta.meta_value AS 'last_name',
Query1.user_email,
Query1.order_item_name
FROM wp_usermeta
INNER JOIN Query1
ON wp_usermeta.user_id = Query1.user_id
WHERE (((wp_usermeta.meta_value) <> '')
AND ((wp_usermeta.meta_key) = 'last_name'));
我尝试通过用括号将Query1的代码括起来,用括号将Query1替换为第二个查询,但这不起作用.我收到此错误:
您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以获取在’.user_id附近使用正确的语法,(在第22行,选择wp_usermeta.user_id,wp_usermeta.meta_value AS为“ first”
有人知道怎么做这个吗?谢谢!!
更新
根据@Hogan提供的答案,我想到了:
SELECT
first.user_id,
first.meta_value AS 'first_name',
last.meta_value AS 'last_name',
Wp_woocommerce_downloadable_product_permissions_1.user_email,
Wp_woocommerce_order_items_1.order_item_name
FROM Wp_woocommerce_order_items
INNER JOIN Wp_woocommerce_downloadable_product_permissions
ON Wp_woocommerce_order_items.order_id = Wp_woocommerce_downloadable_product_permissions.order_id,
wp_usermeta AS last
INNER JOIN ((Wp_woocommerce_order_items AS Wp_woocommerce_order_items_1
INNER JOIN Wp_woocommerce_downloadable_product_permissions AS Wp_woocommerce_downloadable_product_permissions_1
ON Wp_woocommerce_order_items_1.order_id = Wp_woocommerce_downloadable_product_permissions_1.order_id)
INNER JOIN wp_usermeta AS first
ON Wp_woocommerce_downloadable_product_permissions_1.user_id = first.user_id)
ON last.user_id = Wp_woocommerce_downloadable_product_permissions_1.user_id
GROUP BY first.user_id,
first.meta_value,
last.meta_value,
Wp_woocommerce_downloadable_product_permissions_1.user_email,
Wp_woocommerce_order_items_1.order_item_name,
Wp_woocommerce_order_items_1.order_item_type,
first.meta_key,
last.meta_key
HAVING (((Wp_woocommerce_order_items_1.order_item_type) = 'line_item')
AND ((first.meta_key) = 'first_name')
AND ((last.meta_key) = 'last_name'));
它在MS Access中工作得很好,但在SQL中却不能.我收到此错误:
“具有子句”中的未知列“ first.meta_key”
为什么?
我还必须提到,当我将meta_key [s]添加到SELECT时,如下所示:
SELECT
first.user_id,
first.meta_value AS 'first_name',
last.meta_value AS 'last_name',
Wp_woocommerce_downloadable_product_permissions_1.user_email,
Wp_woocommerce_order_items_1.order_item_name,
first.meta_key,
last.meta_key
我至少没有收到任何错误,查询正在运行,但是鉴于我正在本地主机上工作,因此它会不断增加CPU使用率.
解
SELECT DISTINCT达到目的.没有分组.查询结果出现在127.76毫秒内.这是我的查询:
SELECT DISTINCT
first.user_id AS 'id',
first.meta_value AS 'first_name',
last.meta_value AS 'last_name',
Wp_woocommerce_downloadable_product_permissions_1.user_email AS 'email',
Wp_woocommerce_order_items_1.order_item_name AS 'order'
FROM Wp_woocommerce_order_items
INNER JOIN Wp_woocommerce_downloadable_product_permissions
ON Wp_woocommerce_order_items.order_id = Wp_woocommerce_downloadable_product_permissions.order_id,
((Wp_woocommerce_order_items AS Wp_woocommerce_order_items_1
INNER JOIN Wp_woocommerce_downloadable_product_permissions AS Wp_woocommerce_downloadable_product_permissions_1
ON Wp_woocommerce_order_items_1.order_id = Wp_woocommerce_downloadable_product_permissions_1.order_id)
INNER JOIN wp_usermeta AS first
ON Wp_woocommerce_downloadable_product_permissions_1.user_id = first.user_id)
INNER JOIN wp_usermeta AS last
ON Wp_woocommerce_downloadable_product_permissions_1.user_id = last.user_id
WHERE (((first.meta_key) = 'first_name')
AND ((last.meta_key) = 'last_name')
AND ((Wp_woocommerce_order_items_1.order_item_type) = 'line_item'));
如@Hogan所述,只需再次使用另一个别名添加表wp_usermeta,然后进行INNER JOIN.
解决方法:
添加此联接
INNER JOIN wp_usermeta AS last
ON Wp_woocommerce_downloadable_product_permissions_1.user_id = last.user_id AND last.meta_key = 'last_name'
和专栏
last.meta_value AS 'last_name',
您可能不需要对()如此疯狂-顺序无关紧要,除非您正在执行子查询(不是)或混合左右联接(不是).
本文标题为:mysql-WordPress SQL查询产品X的用户名和姓氏?
基础教程推荐
- pbootcms二次开发必须要了解的后台目录结构 2023-07-09
- dedecms根据来访IP区域自动跳转对应页面的方法 2022-07-21
- dedecms织梦全局变量调用方法总结 2023-07-08
- 织梦DedeCMS搜索指定多个栏目文档的办法 2022-11-11
- 织梦dedecms调用当前栏目文章数的方法 2022-11-08
- PbootCMS网站标题描述等标签限制字数的办法 2023-07-08
- pbootcms网站自动清理runtime缓存方法 2023-07-09
- dedecms织梦cms常用判断语句汇总 2022-06-24
- dedecms织梦无需登录注册可下单购买的修改 2022-11-04
- 织梦dedecms如何在dede:sql中使用[field:global.autoindex/] 2022-08-27