nickholt1972 Posted September 23, 2006 Share Posted September 23, 2006 THE PROBLEM: I'm developing an online shop using PHP and MySQL. I've got products and each product might have different sizes, colours, etcI have two tables: [b]products[/b] and [b]productdetails[/b]When i list these in my shop I want it to say (e.g.)[i]Product 1:Colour: white, Size: smallColour white, Size: large[/i]But what i'm getting is[i]Product 1:Colour: white, Size: smallProduct 1:Colour white, Size: large[/i]This is my SQL Query and the PHP to output it:$result = @mysql_query('SELECT products.id, products.productid, products.productname, productdetails.id, productdetails.size FROM productdetails RIGHT JOIN products ON products.productid = productdetails.productid');// Display the text of each entry in a table while ($row = mysql_fetch_array($result)) { echo '<div id="newproduct"><table border="1">' . '<tr><td>' . $row['id'] . '</td><td>' . $row['productid'] . '</td><td>' . $row['productname'] . '</td></tr>' . '<tr><td>' . $row['size'] . '</td></tr></table></div>';}I'm sure its very simple, it usually is. Any help would be greatly appreciated.Thanks,Nick Link to comment https://forums.phpfreaks.com/topic/21783-problem-with-joins-or-group-bys-i-think-im-fairly-new-to-mysql/ Share on other sites More sharing options...
alpine Posted September 23, 2006 Share Posted September 23, 2006 This i believe is one way:SELECT products.id, products.productid, products.productname, productdetails.id, productdetails.size FROM productdetails, products WHERE products.productid = productdetails.productid Link to comment https://forums.phpfreaks.com/topic/21783-problem-with-joins-or-group-bys-i-think-im-fairly-new-to-mysql/#findComment-97290 Share on other sites More sharing options...
fenway Posted September 25, 2006 Share Posted September 25, 2006 Well, if you flip the tables and do a left JOIN, your first idea will work... but you'll have to have a seen hash. Link to comment https://forums.phpfreaks.com/topic/21783-problem-with-joins-or-group-bys-i-think-im-fairly-new-to-mysql/#findComment-97936 Share on other sites More sharing options...
nickholt1972 Posted September 25, 2006 Author Share Posted September 25, 2006 ok, i have 2 questions in reply to that...firstly,are you saying there's a difference betweentable1 RIGHT JOIN ON table2andtable2 LEFT JOIN ON table 1and secondly, what's a 'seen hash'?Many thanks,Nick. Link to comment https://forums.phpfreaks.com/topic/21783-problem-with-joins-or-group-bys-i-think-im-fairly-new-to-mysql/#findComment-98063 Share on other sites More sharing options...
fenway Posted September 25, 2006 Share Posted September 25, 2006 1 - LEFT JOIN is used by convention, where as RIGHT JOIN shouldn't even exist, and probably is only still around for compatability.2 - You have to keep track of the products you've already "seen". Link to comment https://forums.phpfreaks.com/topic/21783-problem-with-joins-or-group-bys-i-think-im-fairly-new-to-mysql/#findComment-98140 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.