我希望为每个用户显示一个订单项,无论他们是否具有wpcf-dc-license-number,wpcf-dc-license-expiration等值。
目前,我的搜索结果仅显示已完成所有字段的用户的用户订单项。
SELECT lx_usermeta.meta_value AS dc_license_number,
lx_usermeta2.meta_value AS dc_license_expiration,
lx_usermeta3.meta_value AS md_license_number,
lx_usermeta4.meta_value AS md_license_expiration,
lx_usermeta5.meta_value AS va_license_number,
lx_usermeta6.meta_value AS va_license_expiration,
lx_usermeta7.meta_value AS first_name,
lx_usermeta8.meta_value AS last_name,
lx_usermeta9.meta_value AS ltx_company
FROM lx_usermeta
JOIN lx_usermeta lx_usermeta2 ON lx_usermeta.user_id = lx_usermeta2.user_id
JOIN lx_usermeta lx_usermeta3 ON lx_usermeta.user_id = lx_usermeta3.user_id
JOIN lx_usermeta lx_usermeta4 ON lx_usermeta.user_id = lx_usermeta4.user_id
JOIN lx_usermeta lx_usermeta5 ON lx_usermeta.user_id = lx_usermeta5.user_id
JOIN lx_usermeta lx_usermeta6 ON lx_usermeta.user_id = lx_usermeta6.user_id
JOIN lx_usermeta lx_usermeta7 ON lx_usermeta.user_id = lx_usermeta7.user_id
JOIN lx_usermeta lx_usermeta8 ON lx_usermeta.user_id = lx_usermeta8.user_id
JOIN lx_usermeta lx_usermeta9 ON lx_usermeta.user_id = lx_usermeta9.user_id
WHERE lx_usermeta.meta_key = "wpcf-dc-license-number"
AND lx_usermeta2.meta_key = "wpcf-dc-license-expiration"
AND lx_usermeta3.meta_key = "wpcf-maryland-license-number"
AND lx_usermeta4.meta_key = "wpcf-maryland-license-expiration"
AND lx_usermeta5.meta_key = "wpcf-virginia-license-number"
AND lx_usermeta6.meta_key = "wpcf-virginia-license-expiration"
AND lx_usermeta7.meta_key = "first_name"
AND lx_usermeta8.meta_key = "last_name"
AND lx_usermeta9.meta_key = "wpcf-ltx-company"
我已经尝试将JOIN更改为LEFT JOIN,FULL JOIN,FULL OUTER JOIN,将UNION放在JOIN语句之间,但我没有任何运气找到方法将这些字段加入而不需要全部加入填写了wpcf字段。
感谢您的帮助!
更新: 我测试了这段代码:
SELECT lx_usermeta.meta_value AS first_name, lx_usermeta2.meta_value AS last_name
FROM lx_usermeta
JOIN lx_usermeta lx_usermeta2 ON lx_usermeta.user_id = lx_usermeta2.user_id
WHERE lx_usermeta.meta_key = "first_name"
AND lx_usermeta2.meta_key = "last_name"
这将一个接一个地显示所有用户,即使他们只有first_name或last_name中的一个或者都没有。
什么可能影响wpcf- *字段(自定义字段),以便当我使用相同的语法时,我只收到所有wpcf字段都有数据的结果?
答案 0 :(得分:0)
我尝试了你的建议fubar,以及:
SELECT
um.meta_value AS dc_license_number,
um2.meta_value AS dc_license_expiration,
um3.meta_value AS md_license_number,
um4.meta_value AS md_license_expiration,
um5.meta_value AS va_license_number,
um6.meta_value AS va_license_expiration,
um7.meta_value AS first_name,
um8.meta_value AS last_name,
um9.meta_value AS ltx_company
FROM
lx_usermeta um
LEFT JOIN lx_usermeta um2 ON um.user_id = um2.user_id AND 'wpcf-dc-license-expiration' = um2.meta_key
LEFT JOIN lx_usermeta um3 ON um.user_id = um3.user_id AND 'wpcf-maryland-license-number' = um3.meta_key
LEFT JOIN lx_usermeta um4 ON um.user_id = um4.user_id AND 'wpcf-maryland-license-expiration' = um4.meta_key
LEFT JOIN lx_usermeta um5 ON um.user_id = um5.user_id AND 'wpcf-virginia-license-number' = um5.meta_key
LEFT JOIN lx_usermeta um6 ON um.user_id = um6.user_id AND 'wpcf-virginia-license-expiration' = um6.meta_key
LEFT JOIN lx_usermeta um7 ON um.user_id = um7.user_id AND 'first_name' = um7.meta_key
LEFT JOIN lx_usermeta um8 ON um.user_id = um8.user_id AND 'last_name' = um8.meta_key
LEFT JOIN lx_usermeta um9 ON um.user_id = um9.user_id AND 'wpcf-ltx-company' = um9.meta_key
WHERE
um.meta_key = 'wpcf-dc-license-number';
我现在得到了一些结果,但仅限于我的三个虚拟用户(一个空白公司名称,两个Northwinds)和Cushman Wakefield用户。
here you can see only cushman and the dummies returned
here you can see in wordpress, users of other companies
这些用户是从不同公司和不同表单导入的,并且每个公司的每个用户都适当地组合/更新了数据。我只是说,Cushman Wakefield不应该以任何方式与其他人保持独特。
感谢您的帮助!