Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
MySQL Joining multiple records
Old 09-27-2010, 10:30 AM MySQL Joining multiple records
dartiss's Avatar
Experienced Talker

Latest Blog Post:
November
Posts: 32
Name: David Artiss
Location: Nottingham, UK
Trades: 0
I'm attempting (and failing) to work out how to write a query for a WordPress table.

The table is named wp_postmeta and contains 3 columns that are relevant - post_id, meta_key and meta_value.

First step - to return all meta_value's where the meta_key contains "rating". That bit I can do.

However, for each post_id with a meta_key of "rating" there is a matching record with a meta_key of "product". How do I return the post_id, product meta_value and rating meta_value as one record?

Many thanks,
David.
dartiss is offline
Reply With Quote
View Public Profile Visit dartiss's homepage!
 
 
Register now for full access!
Old 09-27-2010, 10:38 AM Re: MySQL Joining multiple records
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
You need to do a full-outer self-join, so that you can denormalize the results and retrieve either product or rating if the other doesn't exist.
Code:
SELECT
    IFNULL(r.post_id, p.post_id) AS post_id,
    r.meta_value AS rating,
    p.meta_value AS product
FROM wp_postmeta AS r
FULL OUTER JOIN wp_postmeta AS p
    ON r.post_id = p.post_id
    AND r.meta_key = "rating"
    AND p.meta_key = "product"
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE

Last edited by smoseley; 09-27-2010 at 10:40 AM..
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Reply     « Reply to MySQL Joining multiple records
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37898 seconds with 12 queries