MySQL在单个单元格中连接多个用户ID以在另一个表中显示名称

时间:2015-02-13 16:27:49

标签: php mysql wordpress

MySQL让我疯狂。我已经被要求提供Wordpress数据的csv文件。这些帖子有一大堆元条目,而且大多数情况下,我都有好的结果。但我试图将其他作者中的用户ID连接到他们的显示名称(见下文)。我遇到的困难是尝试输出该列中的每个显示名称。

Tracking Number | Post Title    | Post Date  | Primary Author | Other Authors 
1153            | Title of Post | 2013/03/06 |  Tom Smith     | 213, 100, 600

这是我的疑问:

SELECT results.ID as ID, results.post_title as post_title, results.post_date  
as post_date, results.post_content as post_content, u.display_name as 
primary_author, results.other_authors as other_authors
FROM (SELECT m.meta_value as ID,p.post_title as post_title, p.post_date as   
post_date,p.post_content as post_content,m2.meta_value as primary_author, 
m3.meta_value as other_authors
FROM wp_posts p
INNER JOIN wp_postmeta m ON (p.ID = m.post_id)
INNER JOIN wp_postmeta m2 ON (p.ID = m2.post_id)
INNER JOIN wp_postmeta m3 ON (p.ID = m3.post_id)
WHERE  p.post_type='post' AND p.post_status NOT IN ('trash') AND   
(m.meta_key='idea_tracking_num' AND m.meta_value != '')
AND m2.meta_key='primary_author' AND m3.meta_key='other_authors'
ORDER BY p.post_date
DESC) as results
INNER JOIN wp_users u on (u.ID = results.primary_author)
INNER JOIN wp_users u2 on (u2.ID = results.other_authors)

我也试过这个,但它只输出第一个:

SELECT results.ID as ID, results.post_title as post_title, results.post_date    
as post_date, results.post_content as post_content, u.display_name as 
primary_author, u2.display_name as other_authors
FROM (SELECT m.meta_value as ID,p.post_title as post_title, p.post_date as 
post_date,p.post_content as post_content,m2.meta_value as primary_author, 
m3.meta_value as other_authors
FROM wp_posts p
INNER JOIN wp_postmeta m ON (p.ID = m.post_id)
INNER JOIN wp_postmeta m2 ON (p.ID = m2.post_id)
INNER JOIN wp_postmeta m3 ON (p.ID = m3.post_id)
WHERE  p.post_type='post' AND p.post_status NOT IN ('trash') AND 
(m.meta_key='idea_tracking_num' AND m.meta_value != '')
AND m2.meta_key='primary_author' AND m3.meta_key='other_authors'
ORDER BY p.post_date
DESC) as results
INNER JOIN wp_users u on (u.ID = results.primary_author)
INNER JOIN wp_users u2 on (u2.ID = results.other_authors)

0 个答案:

没有答案