If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > how to turn result rows from JOIN to result columns ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-10, 12:34
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
how to turn result rows from JOIN to result columns ?

My database has different product types that need flexibility for product properties, so the properties are stored in a product_properties table.
I need to retrieve a list of products for each product_type, so the JOIN below naturally returns two result rows for each product, because product type 3 has a set of 2 properties.
Code:
SELECT
		products.productId
	,	products.productTypeId
	,	types.moduleName
	,	products.workflowCode
	,	product_properties.productPropertyId
	,	products.active

FROM
	products
	INNER JOIN
		product_properties
	ON
		product_properties.productId = products.productId

	INNER JOIN
		types
	ON
		types.productTypeId = products.productTypeId

WHERE
	products.productTypeId = 3
	AND
	products.productId = 1
How do I need to change my querry-text to receive one row for each product that has a column for each property to hold the properties' values?

Thanks for any help,
David

Last edited by vivoices; 04-21-10 at 12:37.
Reply With Quote
  #2 (permalink)  
Old 04-21-10, 14:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
and different product types other than type 3 have different properties other than 2?

my advice: do the reformatting in your front-end application language

use the database for efficient storage and retrieval, not re-arranging data into layouts that you find more pleasing
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-21-10, 15:04
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Thanks for your advice, Rudy.
Here is the code that does what I intended:
Code:
SELECT
		products.productId
	,	products.productTypeId
	,	types.moduleName
	,	products.workflowCode
	,	GROUP_CONCAT( product_properties.productPropertyId  SEPARATOR ', ')
	,	products.active

FROM
	products
	INNER JOIN
		product_properties
	ON
		product_properties.productId = products.productId

	INNER JOIN
		types
	ON
		types.productTypeId = products.productTypeId

WHERE
	products.productTypeId = 3
	AND
	products.productId = 1

GROUP BY
	products.productId
but this is just one of several levels of nesting and lookups for one product.
So the question is shifted to performance.
MySQL is good at db queries and should perform better (faster, less memory) than a series of queries from php and "gluing" the data-sets together in php, or not?

[I bought your book and will give you feedback when I start reading]

David
Reply With Quote
  #4 (permalink)  
Old 04-21-10, 16:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by vivoices View Post
MySQL is good at db queries and should perform better (faster, less memory) than a series of queries from php and "gluing" the data-sets together in php, or not?
in general, yes, very much better
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
join, rows to columns

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On