Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 3rd, 2008, 09:07 AM
Newbie
 
Join Date: Sep 2008
Posts: 1
Default Problem with subquery in MySQL

Hello,
I've got following query:

Code:
SELECT y.c_node_id, y.c_code_id, y.c_rule_id
FROM 
	CODE c2
	JOIN (
		SELECT c.c_id AS c_code_id, c.c_code, r.c_id AS c_rule_id, r.c_node_id, 1 AS 'priority'
		FROM
			CODE c
			JOIN HRY h ON h.c_dimension_id = c.c_dimension_id
			JOIN NODE n ON h.c_id = n.c_hierarchy 
			JOIN RULE r ON n.c_id = r.c_node_id
		WHERE h.c_id = 1 AND r.c_value = c.c_code AND r.c_rule_type = 'SR'

		UNION

		SELECT c.c_id AS c_code_id, c.c_code, r.c_id, r.c_node_id, 2 AS 'priority'
		FROM
			CODE c
			JOIN HRY h ON h.c_dimension_id = c.c_dimension_id
			JOIN NODE n ON h.c_id = n.c_hierarchy 
			JOIN RULE r ON n.c_id = r.c_node_id
		WHERE h.c_id = 1 AND (c.c_code BETWEEN r.c_value AND r.c_to_value) AND r.c_rule_type = 'RR'
		
	) AS y ON y.c_code_id = c2.c_id
WHERE y.priority = (
	SELECT MIN(priority)
	FROM (
		SELECT r.c_id, 1 AS 'priority'
		FROM
			NODE n
			JOIN RULE r ON n.c_id = r.c_node_id
		WHERE n.c_hierarchy = 1 AND r.c_value = c2.c_code AND r.c_rule_type = 'SR'
		
		UNION

		SELECT r.c_id, 2 AS 'priority'
		FROM
			NODE n
			JOIN RULE r ON n.c_id = r.c_node_id
		WHERE n.c_hierarchy = 1 AND (c2.c_code BETWEEN r.c_value AND r.c_to_value) AND r.c_rule_type = 'RR'
	) z)
In SQL Server this query executes without any problems but in MySQL it complains that column 'c2.c_code_id' in 'where clause' is unknown (line 31). What is going on? c2 is in the FROM clause of the outer query.

Thanks in advance.

morhen
Reply
  #2  
Old September 3rd, 2008, 01:35 PM
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 304
Default

Quote:
Originally Posted by morhen

WHERE n.c_hierarchy = 1 AND r.c_value = c2.c_code AND r.c_rule_type = 'SR'

UNION

SELECT r.c_id, 2 AS 'priority'
FROM
NODE n
JOIN RULE r ON n.c_id = r.c_node_id
WHERE n.c_hierarchy = 1 AND (c2.c_code BETWEEN r.c_value AND r.c_to_value) AND r.c_rule_type = 'RR'
) z)
[/code]

In SQL Server this query executes without any problems but in MySQL it complains that column 'c2.c_code_id' in 'where clause' is unknown (line 31). What is going on? c2 is in the FROM clause of the outer query.

Thanks in advance.

morhen
You are saying that the error is with column c2.c_code_id but in your line 31 I only see c2.c_code . But maybe this is not a mistake in your query, but only an error in the description of your problem?

Mysql handles things differently than MSSQL. What version of Mysql are you using?

Have you tried to replace c2.c_code with CODE.c_code ? Do you still get the error? In other words, why not refer to the table CODE directly here, instead of using the alias "c2" which you have given to it in the outer query?
Reply
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles