联合查询中的排序错误

时间:2010-06-30 07:01:12

标签: mysql group-by union sql-order-by

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

在我对所选字段进行排序时,此查询出了什么问题。我在“订单子句” 中收到错误“ #1054 - 未知列'rt.d_abd'。 请帮帮我

3 个答案:

答案 0 :(得分:1)

错误是你将tablename.columnname 放在反引号之间:MySQL将其解释为一个列名。

要么使用`tablename` .columnname`,要么根本不使用反对。

答案 1 :(得分:1)

ORDER BY `rt.d_abd` ASC

应该是

ORDER BY `rt`.`d_abd` ASC

ORDER BY rt.d_abd ASC

EDITED 你可以使用单引号工作

ORDER BY 'rt.d_abd' ASC

我认为您没有使用正确的语法()正确使用

它应该如下

(SELECT column1. column2
FROM table1)
UNION ALL
(SELECT column3, column4
FROM table2)
ORDER BY column1

答案 2 :(得分:0)

尝试此查询:

(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