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 > subquery result as select_expression ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-22-10, 10:46
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
subquery result as select_expression ?

I have the following code, that does only half of what I want it to, but executes without error
Code:
SELECT
		properties.productId
	,	properties.propertyName
	,	finishes.workflowCode
FROM
	finishes
	INNER JOIN
		(		
			SELECT
					product_properties.productId AS productId
				,	property_definitions.productPropertyName AS propertyName
				,	product_properties.productPropertyValue AS propertyValue
				,	property_definitions.productPropertyLokkupTableName AS tableName

			FROM
				product_properties
				INNER JOIN
					property_definitions
				ON
					product_properties.productPropertyId = property_definitions.productPropertyId
		) AS properties
	ON
		finishes.productFinishId = properties.propertyValue
I want it to do something like this:
Code:
SELECT
		properties.productId
	,	properties.propertyName
	,	finishes.workflowCode
FROM
	CONCAT( properties.tableName )
	INNER JOIN
		(		
			SELECT
					product_properties.productId AS productId
				,	property_definitions.productPropertyName AS propertyName
				,	product_properties.productPropertyValue AS propertyValue
				,	property_definitions.productPropertyLokkupTableName AS tableName

			FROM
				product_properties
				INNER JOIN
					property_definitions
				ON
					product_properties.productPropertyId = property_definitions.productPropertyId
		) AS properties
	ON
		CONCAT( properties.tableName, properties.propertyName ) = properties.propertyValue
but of course the CONCAT() does not convert the result from the subquerry, which is executed first and should be available for the main querry, from a string into an expression for the FROM and ON clauses.

Any idea how to achieve this?

Thanks,
David
Reply With Quote
  #2 (permalink)  
Old 04-22-10, 11:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
i think you've over-generalized yourself right into a corner

why are you storing a table name? why is the table a variable for the query?

why can't you just store everything that belongs together in one table?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-22-10, 11:16
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Quote:
Originally Posted by r937 View Post
i think you've over-generalized yourself right into a corner
You are right if the corner has no way out
Quote:
Originally Posted by r937 View Post
why are you storing a table name? why is the table a variable for the query?

why can't you just store everything that belongs together in one table?
because I have different product types each with a different, mostly overlapping set of properties. Some properties are predefined, hence the stored table name, and some will read a direct value from a table higher up in the tree.
I need this level of generalization to allow for future product types with a yet unknown set of properties.

Thanks for your reply,
David
Reply With Quote
  #4 (permalink)  
Old 04-22-10, 11:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
you'll probably want to run the subquery by itself, and then tailor a second query based on that
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-22-10, 11:29
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
I tried that, but did not find a way of letting the following query loop over the results of the first.
The biggest problem so far is writing a variable query syntax.
FROM appears not to accept an @tableName variable etc.
Reply With Quote
  #6 (permalink)  
Old 04-22-10, 14:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by vivoices View Post
FROM appears not to accept an @tableName variable etc.
yup, and a prepared statement EXECUTE cannot use a placeholder for a table name, only data values

you'll have to have an "if" block and a separate query for every table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 04-23-10, 14:06
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
I tried to solve the problem by generalizing even more and put all properties for all product types into a single table.
That would have been possible, but I stopped when I saw that the data itself would be completely unreadable and very hard to maintain event through SQL / PHP.
So I generalized less, like you suggested and created a property table for every product type and it works like a charm.
Now with every new product-type, a new properties table has to be defined for the new product. That is OK and less work than generalizing the whole system.

Thanks a lot,
David
Reply With Quote
Reply

Tags
subquery

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