我遇到了用于显示自定义配置文件字段和任何(可选)相应值的SQL查询的问题.
这是我正在使用的SQL查询:
SELECT pf.`id`, pf.`name`, pv.`value` FROM `profile_fields` AS pf LEFT JOIN `profile_values` AS pv ON (pf.`id` = pv.`field_id`) WHERE (pf.`site_id` = '0' OR pf.`site_id` = '%d') AND (pv.`user_id` = '%d' OR pv.`user_id` IS NULL) ORDER BY pf.`order` ASC
我遇到的问题是,任何没有相应profile_values
记录的列都不显示,只显示它们,但只显示空值.
非常感谢!
尝试将配置文件值条件移动到JOIN语句:
SELECT pf.`id`, pf.`name`, pv.`value` FROM `profile_fields` AS pf LEFT JOIN `profile_values` AS pv ON ( pf.`id` = pv.`field_id` AND (pv.`user_id` = '%d' OR pv.`user_id` IS NULL) ) WHERE (pf.`site_id` = '0' OR pf.`site_id` = '%d') ORDER BY pf.`order` ASC