将where子句更改为or
SELECT P.ID, P.post_title, P.post_content, P.post_author, meta_value
FROM wp_posts AS P
LEFT JOIN wp_postmeta AS PM on PM.post_id = P.ID
WHERE P.post_type = \'post\' and P.post_status = \'publish\' and ( meta_key = \'job_title\' or meta_key = \'company_email\' )
ORDER BY P.post_date DESC
但是这个sql将在2行中为您的元键和值返回相同的帖子;你会得到这样的结果
+-----+------------+--------------+-------------+----------+------------+
| ID | post_title | post_content | post_author | meta_key | meta_value |
+-----+------------+--------------+-------------+----------+------------+
| 10 | Post Title | Post Content | 1 | key1 | value1 |
+-----+------------+--------------+-------------+----------+------------+
| 10 | Post Title | Post Content | 1 | key2 | value2 |
+-----+------------+--------------+-------------+----------+------------+
如果需要将元键作为列,将元值作为对应列的值,则需要将行旋转到列
SELECT P.ID, P.post_title, P.post_content, P.post_author,
MAX(IF(PM.meta_key = \'job_title\', PM.meta_value, NULL)) AS job_title,
MAX(IF(PM.meta_key = \'company_email\', PM.meta_value, NULL)) AS company_email
FROM wp_posts AS P
LEFT JOIN wp_postmeta AS PM on PM.post_id = P.ID
WHERE P.post_type = \'post\' and P.post_status = \'publish\'
ORDER BY P.post_date DESC
这将返回一行结果,如下所示
+-----+------------+--------------+-------------+-----------+---------------+
| ID | post_title | post_content | post_author | job_title | company_email |
+-----+------------+--------------+-------------+-----------+---------------+
| 10 | Post Title | Post Content | 1 | job title | company@email |
+-----+------------+--------------+-------------+-----------+---------------+