问题
I need some help making the second join with the tables below. I got some help from here previously where it was suggested I need to add a second JOIN, however, this is where I'm stuck and need some assistance.
wp-posts
-----------------
id | Post_Title |
-----------------
01 | Event 1 |
-----------------
02 | Event 2 |
-----------------
wp-postmeta
-------------------------------------------------------
meta_id | post_id | meta_key | meta_value |
-----------------------------------------------------
456 | 01 | _EventStartDate | 01/01/2017 |
-------------------------------------------------- ---
789 | 01 | _EventEndDate | 05/02/2017 |
-----------------------------------------------------
The end result I'm after is something like;
Title - starts on <_EventStartDate > and ends on <_EventEndDate>
and im using the following to get the data:
$result = $wpdb->get_results ( "
SELECT $wpdb->posts.ID, $wpdb->posts.post_content, $wpdb->postmeta.meta_id,
$wpdb->postmeta.post_id, $wpdb->postmeta.meta_key, $wpdb->postmeta.meta_value, $wpdb->posts.post_title
FROM $wpdb->posts INNER JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id
WHERE $wpdb->postmeta.meta_key = '_EventStartDate'
ORDER BY $wpdb->postmeta.meta_value " );
Now I've been told that the WHERE will just return a single row and that I need to make the second JOIN using $wpdb->postmeta.meta_key = '_EventStartDate'
but after hours of trying to implement this I'm unable to get and data back at all.
If someone could help solve this it would be immensely helpful as I have a few more queries I would like to write and I'm guessing I will need to use the same principal with them too.
Thanks for reading!!
回答1:
You can do it with a single inner join, grouping by the post ID and then splitting start date and end date:
select
wpposts.post_content,
substring_index(GROUP_CONCAT(meta_value order by str_to_date(meta_value,'%d/%m/%Y')), ',', 1) as start_date ,
substring_index(GROUP_CONCAT(meta_value order by str_to_date(meta_value,'%d/%m/%Y')), ',', -1) as end_date
from wpposts inner join wppostmeta
on wpposts.id = wppostmeta.post_id
where wppostmeta.meta_key='_EventStartDate' or wppostmeta.meta_key='_EventEndDate'
group by wppostmeta.post_id
回答2:
You can get the start_date and end_date in one row using group by
like this:
select
t1.*,
max(case when meta_key = '_EventStartDate' then meta_value end) start_date,
max(case when meta_key = '_EventEndDate' then meta_value end) end_date
from wp_posts t1
inner join wp_postmeta t2
on t1.id = t2.post_id
group by t1.post_id;
if ONLY_FULL_GROUP_BY
is enabled, change the group by clause to:
group by t1.post_id, t1.Post_title;
回答3:
Assuming there is only one _EventStartDate
and one _EventEndDate
per post in wp-postmeta, a simplified query would be:
SELECT p.post_title, pm1.meta_value AS start_date, pm2.meta_value AS end_date
FROM wp-posts p
INNER JOIN wp-postmeta pm1
ON p.post_id = pm1.post_id
AND pm1.meta_ekey = '_EventStartDate'
INNER JOIN wp-postmeta pm2
ON p.post_id = pm2.post_id
AND pm2.meta_ekey = '_EventEndDate'
来源:https://stackoverflow.com/questions/41891324/mysql-muliple-joins-from-same-colums-with-different-keys