[英]How can I get a SQL Query from wordpress tables
I have a sql query and don't find the right solution.我有一个 sql 查询,但没有找到正确的解决方案。
The query works, but it doesn't show me the columns of the wp_usermeta
table, only the columns of the wp_users
are shown.查询有效,但它没有显示wp_usermeta
表的列,只显示wp_users
的列。
If I delete the where
clause, all lines with all values of the columns are shown, but I need only the lines depending on the meta_value = "Prod"
from the column meta.key = 'kennung_produzent'如果我删除where
子句,将显示所有列的所有值的行,但我只需要取决于meta_value = "Prod"
列中的 meta.key = 'kennung_produzent' 的行
SELECT p.ID,
p.user_login,
p.user_registered,
p.display_name,
max( CASE WHEN pm.meta_key = 'nickname' and p.ID = pm.user_id THEN pm.meta_value END ) as Nickname,
max( CASE WHEN pm.meta_key = 'billing_email' and p.ID = pm.user_id THEN pm.meta_value END ) as email,
max( CASE WHEN pm.meta_key = 'billing_first_name' and p.ID = pm.user_id THEN pm.meta_value END ) as Vorname,
max( CASE WHEN pm.meta_key = 'billing_last_name' and p.ID = pm.user_id THEN pm.meta_value END ) as Nachname,
max( CASE WHEN pm.meta_key = 'billing_phone' and p.ID = pm.user_id THEN pm.meta_value END ) as Telefon,
max( CASE WHEN pm.meta_key = 'billing_postcode' and p.ID = pm.user_id THEN pm.meta_value END ) as PLZ,
max( CASE WHEN pm.meta_key = 'billing_address_1' and p.ID = pm.user_id THEN pm.meta_value END ) as Strasse,
max( CASE WHEN pm.meta_key = 'billing_city' and p.ID = pm.user_id THEN pm.meta_value END ) as Ort,
max( CASE WHEN pm.meta_key = 'kennung_produzent' and p.ID = pm.user_id THEN pm.meta_value END ) as Kennung
from
wp_users p
join wp_usermeta pm on p.ID = pm.user_id
where
pm.meta_value = 'Prod'
group by
p.ID
If I understand correctly, you want to filter the result to only the ones for which in wp_usermeta
there is row with meta_key='kennung_produzent'
and meta_value = 'Prod'
如果我理解正确,您只想将结果过滤为wp_usermeta
中存在meta_key='kennung_produzent'
和meta_value = 'Prod'
行
What that query is doing is different, because you are filtering by pm.meta_value = 'Prod'
so you are removing all the rows because it also expects the value to be Prod when pm.meta_key = 'billing_email'
, and when pm.meta_key = 'nickname'
and so on.该查询所做的是不同的,因为您正在按pm.meta_value = 'Prod'
进行过滤,因此您要删除所有行,因为它还期望在pm.meta_key = 'billing_email'
和pm.meta_key = 'nickname'
时值是 Prod pm.meta_key = 'nickname'
等等。
Not knowing your data, and based on your last comment, assuming you have more than one row per user with meta_key="kennung_produzent", I would say that what you want to do is more similar to不知道您的数据,并且根据您的最后评论,假设每个用户有超过一行 meta_key="kennung_produzent",我会说您想要做的更类似于
select * from (
SELECT p.ID,
p.user_login,
p.user_registered,
p.display_name,
max( CASE WHEN pm.meta_key = 'nickname' and p.ID = pm.user_id THEN pm.meta_value END ) as Nickname,
max( CASE WHEN pm.meta_key = 'billing_email' and p.ID = pm.user_id THEN pm.meta_value END ) as email,
max( CASE WHEN pm.meta_key = 'billing_first_name' THEN pm.meta_value END ) as Vorname,
max( CASE WHEN pm.meta_key = 'billing_last_name' THEN pm.meta_value END ) as Nachname,
max( CASE WHEN pm.meta_key = 'billing_phone' THEN pm.meta_value END ) as Telefon,
max( CASE WHEN pm.meta_key = 'billing_postcode' THEN pm.meta_value END ) as PLZ,
max( CASE WHEN pm.meta_key = 'billing_address_1' THEN pm.meta_value END ) as Strasse,
max( CASE WHEN pm.meta_key = 'billing_city' THEN pm.meta_value END ) as Ort,
max( CASE WHEN pm.meta_key = 'kennung_produzent' THEN pm.meta_value END ) as Kennung
from
wp_users p
join wp_usermeta pm on p.ID = pm.user_id
group by
p.ID
) a
where Kennung LIKE "Prod"
the performance of this query can be very bad, but please let me know if it does what you need so at least we have a starting point此查询的性能可能非常糟糕,但请让我知道它是否满足您的需求,因此至少我们有一个起点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.