使用元表中的多个条目的 Compex MySQL 左连接
我正在尝试创建一个查询来从主表(WordPress 用户表)和用户元表中获取信息。
如果您不熟悉 WP DB 架构:wp_users 保存基本用户信息及其 ID,wp_usermeta 保存 ID、meta_key 和 meta_value。
假设我想要获取具有特定元键和元值的所有用户。这是我当前的查询(根据可用的元密钥通过 PHP 生成):
SELECT ID,
user_email,
user_login,
first_name.meta_value as first_name,
last_name.meta_value as last_name,
phone_number.meta_value as phone_number,
wp_capabilities.meta_value as wp_capabilities
FROM wp_users,
(select * from wp_usermeta where meta_key = 'first_name') as first_name,
(select * from wp_usermeta where meta_key = 'last_name') as last_name,
(select * from wp_usermeta where meta_key = 'phone_number') as phone_number,
(select * from wp_usermeta where meta_key = 'wp_capabilities') as wp_capabilities
WHERE
ID = first_name.user_id AND
ID = last_name.user_id AND
ID = phone_number.user_id AND
ID = wp_capabilities.user_id AND
wp_wpi_capabilities.meta_value LIKE '%administrator%'
ORDER BY first_name
这做得很好,但是如果某个用户缺少元密钥,例如“last_name”,则根本不会返回该用户行。所以我真正需要的是一种为丢失的元键返回 Null 值的方法。
现在我有一个非常hackish的函数,它会为所有没有给定meta_key的用户创建空白的meta_keys和meta_values,这样它们就会被返回。当您有超过一千个用户并且需要添加新类型的meta_key 时,这是一种糟糕的方法。
如果有人这样做过,或者我是否需要更好地解释,请告诉我。
谢谢。
I am trying to create a single query for getting information from the main table (WordPress user table), and the user meta table.
If you're not familiar with WP DB architecture: wp_users holds basic user information and their IDs, wp_usermeta holds the ID, meta_key, and meta_value.
Let's say I want to get all users that have certain meta_keys and meta_values. This is the query I currently have (generated via PHP based on available meta_keys):
SELECT ID,
user_email,
user_login,
first_name.meta_value as first_name,
last_name.meta_value as last_name,
phone_number.meta_value as phone_number,
wp_capabilities.meta_value as wp_capabilities
FROM wp_users,
(select * from wp_usermeta where meta_key = 'first_name') as first_name,
(select * from wp_usermeta where meta_key = 'last_name') as last_name,
(select * from wp_usermeta where meta_key = 'phone_number') as phone_number,
(select * from wp_usermeta where meta_key = 'wp_capabilities') as wp_capabilities
WHERE
ID = first_name.user_id AND
ID = last_name.user_id AND
ID = phone_number.user_id AND
ID = wp_capabilities.user_id AND
wp_wpi_capabilities.meta_value LIKE '%administrator%'
ORDER BY first_name
This does a good job, however if a certain user is missing a meta_key, 'last_name' for instance, that user row does not get returned at all. So really all I need is a way of returning a Null value for missing meta keys.
Right now I have a very hackish function that goes through and creates blank meta_keys and meta_values for all the users that don't have a given meta_key, that way they are returned. This is a terrible way of doing it though when you have over a thousand users, and you need to add a new type of meta_key.
Let me know if anybody has done this, or if I need to explain better.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将用户元连接标准放入连接中,而不是费力地处理子查询:
Put the usermeta joining criteria in the join rather than struggling with subqueries: