Home > Net >  SQL Join query from two table
SQL Join query from two table

Time:06-28

I am trying to do SQL query with JOIN. But without success.

I am trying to sort this query

SELECT ID, post_parent FROM wp_posts WHERE `post_type`='wc_booking'

With this (_booking_start ascending).

SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE meta_key='_booking_start'

Question how would JOIN query look in this case?

CodePudding user response:

I believe you simply need to do this:

SELECT wp_posts.ID, wp_posts.post_parent, wp_postmeta.meta_key, wp_postmeta.meta_value
FROM wp_posts
LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key = '_booking_start'
WHERE wp_posts.post_type = 'wc_booking'
  • Related