HowardMarks
MIS
Dear all,
hopefully some one can help me with this, i've been banging my head against it all day.
My table structure is as follows:
listing_options:
loption_id, loption_key
1, "title"
2, "maindesc"
3, "mainimage"
listing_user_options:
lo_listingid, lo_loptionid, lo_value
1343, 1, "This is the title"
1343, 2, "This is the Description"
1343, 3, "This is the image"
listings:
listing_id
1343
So I want to query all of the listings and pull out any listing_user_options and, based on their associated listing_option, assign them to one results set
something like:
SELECT listings.listing_id FROM listings,
(lo_value FROM listing_user_options LEFT JOIN listing_options ON lo_loptionid = loption_id WHERE loption_key = 'title' AND lo_listingid = listings.listing_id) As Title,
(lo_value FROM listing_user_options LEFT JOIN listing_options ON lo_loptionid = loption_id WHERE loption_key = 'maindesc' AND lo_listingid = listings.listing_id) As MainDescription,
(lo_value FROM listing_user_options LEFT JOIN listing_options ON lo_loptionid = loption_id WHERE loption_key = 'mainimage' AND lo_listingid = listings.listing_id) As MainImage
ORDER BY Title
So my results set end up with
listing["listing_id"] = 1343
listing["Title"] = "This is the title"
listing["MainDescription"] = "This is the Description"
listing["MainImage"] = "This is the image"
I've tried all manner of joins, subqueries, table aliases etc but my lack of knowledge of more complex SQL is failing me. Could any one point me in the right direction?
Many thanks
Nick
Nick (Webmaster)
hopefully some one can help me with this, i've been banging my head against it all day.
My table structure is as follows:
listing_options:
loption_id, loption_key
1, "title"
2, "maindesc"
3, "mainimage"
listing_user_options:
lo_listingid, lo_loptionid, lo_value
1343, 1, "This is the title"
1343, 2, "This is the Description"
1343, 3, "This is the image"
listings:
listing_id
1343
So I want to query all of the listings and pull out any listing_user_options and, based on their associated listing_option, assign them to one results set
something like:
SELECT listings.listing_id FROM listings,
(lo_value FROM listing_user_options LEFT JOIN listing_options ON lo_loptionid = loption_id WHERE loption_key = 'title' AND lo_listingid = listings.listing_id) As Title,
(lo_value FROM listing_user_options LEFT JOIN listing_options ON lo_loptionid = loption_id WHERE loption_key = 'maindesc' AND lo_listingid = listings.listing_id) As MainDescription,
(lo_value FROM listing_user_options LEFT JOIN listing_options ON lo_loptionid = loption_id WHERE loption_key = 'mainimage' AND lo_listingid = listings.listing_id) As MainImage
ORDER BY Title
So my results set end up with
listing["listing_id"] = 1343
listing["Title"] = "This is the title"
listing["MainDescription"] = "This is the Description"
listing["MainImage"] = "This is the image"
I've tried all manner of joins, subqueries, table aliases etc but my lack of knowledge of more complex SQL is failing me. Could any one point me in the right direction?
Many thanks
Nick
Nick (Webmaster)