Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with subquery in MySQL

Newbie
 
Join Date: Sep 2008
Posts: 1
#1: Sep 3 '08
Hello,
I've got following query:

Expand|Select|Wrap|Line Numbers
  1. SELECT y.c_node_id, y.c_code_id, y.c_rule_id
  2. FROM 
  3.     CODE c2
  4.     JOIN (
  5.         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'
  6.         FROM
  7.             CODE c
  8.             JOIN HRY h ON h.c_dimension_id = c.c_dimension_id
  9.             JOIN NODE n ON h.c_id = n.c_hierarchy 
  10.             JOIN RULE r ON n.c_id = r.c_node_id
  11.         WHERE h.c_id = 1 AND r.c_value = c.c_code AND r.c_rule_type = 'SR'
  12.  
  13.         UNION
  14.  
  15.         SELECT c.c_id AS c_code_id, c.c_code, r.c_id, r.c_node_id, 2 AS 'priority'
  16.         FROM
  17.             CODE c
  18.             JOIN HRY h ON h.c_dimension_id = c.c_dimension_id
  19.             JOIN NODE n ON h.c_id = n.c_hierarchy 
  20.             JOIN RULE r ON n.c_id = r.c_node_id
  21.         WHERE h.c_id = 1 AND (c.c_code BETWEEN r.c_value AND r.c_to_value) AND r.c_rule_type = 'RR'
  22.  
  23.     ) AS y ON y.c_code_id = c2.c_id
  24. WHERE y.priority = (
  25.     SELECT MIN(priority)
  26.     FROM (
  27.         SELECT r.c_id, 1 AS 'priority'
  28.         FROM
  29.             NODE n
  30.             JOIN RULE r ON n.c_id = r.c_node_id
  31.         WHERE n.c_hierarchy = 1 AND r.c_value = c2.c_code AND r.c_rule_type = 'SR'
  32.  
  33.         UNION
  34.  
  35.         SELECT r.c_id, 2 AS 'priority'
  36.         FROM
  37.             NODE n
  38.             JOIN RULE r ON n.c_id = r.c_node_id
  39.         WHERE n.c_hierarchy = 1 AND (c2.c_code BETWEEN r.c_value AND r.c_to_value) AND r.c_rule_type = 'RR'
  40.     ) 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

Needs Regular Fix
 
Join Date: Mar 2008
Posts: 311
#2: Sep 3 '08

re: Problem with subquery in MySQL


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