您也可以使用subquery
下面提到的查询来执行此操作,如果未提及,它也必须自动考虑为 NO
SELECT
q.company_id,q.company_name,
CASE WHEN q.parking IS NULL THEN 'No' ELSE q.parking END AS parking,
CASE WHEN q.fireexit IS NULL THEN 'No' ELSE q.fireexit END AS fireexit,
CASE WHEN q.gym IS NULL THEN 'No' ELSE q.gym END AS gym
FROM (
SELECT cm.`company_id`,
(SELECT `title` FROM `company` WHERE id=cm.`company_id`) AS company_name,
(SELECT `meta_value` FROM `company_meta` WHERE company_id=cm.`company_id` AND meta_key ='feature_parking') AS parking,
(SELECT `meta_value` FROM `company_meta` WHERE company_id=cm.`company_id` AND meta_key ='feature_fireexit') AS fireexit,
(SELECT `meta_value` FROM `company_meta` WHERE company_id=cm.`company_id` AND meta_key ='feature_gym') AS gym
FROM `company_meta` cm GROUP BY cm.`company_id` ) q
现在在 wordpress 中定义$wpdb
数据库交互的全局变量
global $wpdb;
$results= $wpdb->get_results(" SELECT
q.company_id,q.company_name,
CASE WHEN q.parking IS NULL THEN 'No' ELSE q.parking END AS parking,
CASE WHEN q.fireexit IS NULL THEN 'No' ELSE q.fireexit END AS fireexit,
CASE WHEN q.gym IS NULL THEN 'No' ELSE q.gym END AS gym
FROM (
SELECT cm.`company_id`,
(SELECT `title` FROM `company` WHERE id=cm.`company_id`) AS company_name,
(SELECT `meta_value` FROM `company_meta` WHERE company_id=cm.`company_id` AND meta_key ='feature_parking') AS parking,
(SELECT `meta_value` FROM `company_meta` WHERE company_id=cm.`company_id` AND meta_key ='feature_fireexit') AS fireexit,
(SELECT `meta_value` FROM `company_meta` WHERE company_id=cm.`company_id` AND meta_key ='feature_gym') AS gym
FROM `company_meta` cm GROUP BY cm.`company_id` ) q");
foreach($results as $r){
echo "Company ".$r->company_id ." : ".$r->company_name;
echo "Parking : ".$r->parking;
echo "Fire Exit : ".$r->fireexit;
echo "Gym : ".$r->gym;
}
这是你的小提琴
我希望它适合你