views:

23

answers:

3
SELECT DISTINCT rt . d_rev_id , rt . d_rev_code , rt . d_reason , rt . d_rev_status , rt . d_apb , rt . d_cb , pt . d_partid , pt . d_part_no , pt . d_ab , pt . d_abd , pt . d_status , rt . d_part_name , rt . d_part_desc , rt . d_part_type , pnv . d_pn_val , pnv . d_pn_id , cfv . d_optionname , rt . d_projectid , rt . d_abd , rt . d_apbd 
FROM 
    ( 
        ( 
        design_parts pt 
        INNER JOIN design_part_number_val pnv 
        USING ( d_partid ) 
        INNER JOIN design_revision_temp rt 
        USING ( d_partid ) 
        ) 
    LEFT JOIN design_pn_custom_field_options cfv 
    ON d_optionvalue = d_pn_val 
    )
WHERE rt . d_projectid = 1 
AND 1 
GROUP BY d_rev_id 
UNION ALL 
SELECT DISTINCT dr . d_rev_id , dr . d_rev_code , dr . d_reason , dr . d_part_name , dr . d_apb , dr . d_cb , pt . d_partid , pt . d_part_no , pt . d_ab , pt . d_abd , pt . d_status , dr . d_part_name , dr . d_part_desc , dr . d_part_type , pnv . d_pn_val , pnv . d_pn_id , cfv . d_optionname , dr . d_projectid , dr . d_apbd , dr . d_cbd 
FROM 
    ( 
        ( 
        design_parts pt 
        INNER JOIN design_part_number_val pnv 
        USING ( d_partid ) 
        INNER JOIN design_revision dr 
        USING ( d_partid ) 
        ) 
    LEFT JOIN design_pn_custom_field_options cfv 
    ON d_optionvalue = d_pn_val 
    ) 
WHERE dr . d_projectid = 1 
AND 1 
GROUP BY d_partid  
ORDER BY `rt.d_abd` ASC

What is wrong in this query as I ordering the field that are selected. I am getting an Error "*#1054 - Unknown column 'rt.d_abd' in 'order clause' "*. Please help me

+1  A: 

The error is that you put tablename.columnname between backticks: MySQL interprets that as one columnname.

Either use `tablename`.`columnname` or no backticks at all.

Konerak
I tried this but it not works, I am using alias for table name.
parag
What error does it give with the `\`rt\`.\`d_abd\`` then?
Konerak
+1  A: 
ORDER BY `rt.d_abd` ASC

should be

ORDER BY `rt`.`d_abd` ASC

or

ORDER BY rt.d_abd ASC

EDITED you can use single quotes also work

ORDER BY 'rt.d_abd' ASC

I think you are not using proper syntax use ( and ) properly

it should as follow

(SELECT column1. column2
FROM table1)
UNION ALL
(SELECT column3, column4
FROM table2)
ORDER BY column1
Salil
I tried both of this but it is not worked. It still giving same error
parag
@parag :- use '(' and ')' properly
Salil
A: 

Try this query:

(SELECT DISTINCT rt . d_rev_id , rt . d_rev_code , rt . d_reason , rt . d_rev_status , rt . d_apb , rt . d_cb , pt . d_partid , pt . d_part_no , pt . d_ab , pt . d_abd , pt . d_status , rt . d_part_name , rt . d_part_desc , rt . d_part_type , pnv . d_pn_val , pnv . d_pn_id , cfv . d_optionname , rt . d_projectid , rt . d_abd , rt . d_apbd 
FROM 
    design_parts pt 
    INNER JOIN design_part_number_val pnv 
    USING ( d_partid ) 
    INNER JOIN design_revision_temp rt 
    USING ( d_partid ) 
    LEFT JOIN design_pn_custom_field_options cfv 
    ON d_optionvalue = d_pn_val 
WHERE rt . d_projectid = 1 
GROUP BY d_rev_id)
UNION ALL 
(SELECT DISTINCT dr . d_rev_id , dr . d_rev_code , dr . d_reason , dr . d_part_name , dr . d_apb , dr . d_cb , pt . d_partid , pt . d_part_no , pt . d_ab , pt . d_abd , pt . d_status , dr . d_part_name , dr . d_part_desc , dr . d_part_type , pnv . d_pn_val , pnv . d_pn_id , cfv . d_optionname , dr . d_projectid , dr . d_apbd , dr . d_cbd 
FROM 
    design_parts pt 
    INNER JOIN design_part_number_val pnv 
    USING ( d_partid ) 
    INNER JOIN design_revision dr 
    USING ( d_partid ) 
    LEFT JOIN design_pn_custom_field_options cfv 
    ON d_optionvalue = d_pn_val 
WHERE dr . d_projectid = 1 
GROUP BY d_partid)
ORDER BY `d_abd` ASC
Konerak
I get this error massage Table 'design_revision' from one of the SELECTs cannot be used in global ORDER clause What is the "global ORDER clause"? can u tell me please.
parag
The global order clause is the (only) ORDER BY that is present. It's global because it works on the entire UNION (and not on each select individually). The UNION result has no name, so you can just use ORDER BY d_abd.
Konerak
Ok but it says "d_abd in order clause is ambiguous" then what should i do in terms to solve it
parag
This is because the column exists in two tables you call (CTRL-F to find them). Give one of both an alias.
Konerak