mysql union都没有用

时间:2014-02-11 21:54:01

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

使用下面的MySQL代码和ORDER BY不起作用。我搜索并尝试了各种版本的代码(UNION,UNION ALL,带括号,没有括号,SELECT包装所有SELECT UNION ALL然后排序) - 没有运气。还有其他建议吗?

        (SELECT FORMAT(lt.`rets_111`, 0) AS 'ListPrice', lt.`rets_128` AS 'MlsNumber', lt.`rets_13` AS 'Address', lt.`rets_2044` AS 'PublicRemarks', lt.`rets_sysid`, @real_estate_type:='res' AS real_estate_type,
    lt.`rets_2362` AS 'OpenHouseDate1', lt.`rets_2364` AS 'OpenHouseStartTime1', lt.`rets_2366` AS 'OpenHouseEndTime1', lt.`rets_2370` AS 'DisplayOpenHouse1',
    lt.`rets_2372` AS 'OpenHouseDate2', lt.`rets_2374` AS 'OpenHouseStartTime2', lt.`rets_2376` AS 'OpenHouseEndTime2', lt.`rets_2380` AS 'DisplayOpenHouse2'
    FROM res_listings AS lt
    WHERE lt.`rets_32` IN ('AGENT1', 'AGENT2') OR lt.`rets_2337` IN ('AGENT1', 'AGENT2') OR lt.`rets_1836` IN ('AGENT1', 'AGENT2'))

    UNION ALL

    (SELECT FORMAT(lt.`rets_111`, 0) AS 'ListPrice', lt.`rets_128` AS 'MlsNumber', lt.`rets_13` AS 'Address', lt.`rets_2044` AS 'PublicRemarks', lt.`rets_sysid`, @real_estate_type:='con' AS real_estate_type,
    lt.`rets_2362` AS 'OpenHouseDate1', lt.`rets_2364` AS 'OpenHouseStartTime1', lt.`rets_2366` AS 'OpenHouseEndTime1', lt.`rets_2370` AS 'DisplayOpenHouse1',
    lt.`rets_2372` AS 'OpenHouseDate2', lt.`rets_2374` AS 'OpenHouseStartTime2', lt.`rets_2376` AS 'OpenHouseEndTime2', lt.`rets_2380` AS 'DisplayOpenHouse2'
    FROM con_listings AS lt
    WHERE lt.`rets_32` IN ('AGENT1', 'AGENT2') OR lt.`rets_2337` IN ('AGENT1', 'AGENT2') OR lt.`rets_1836` IN ('AGENT1', 'AGENT2'))

    UNION ALL

    (SELECT FORMAT(lt.`rets_111`, 0) AS 'ListPrice', lt.`rets_128` AS 'MlsNumber', lt.`rets_13` AS 'Address', lt.`rets_2044` AS 'PublicRemarks', lt.`rets_sysid`, @real_estate_type:='mul' AS real_estate_type,
    lt.`rets_2362` AS 'OpenHouseDate1', lt.`rets_2364` AS 'OpenHouseStartTime1', lt.`rets_2366` AS 'OpenHouseEndTime1', lt.`rets_2370` AS 'DisplayOpenHouse1',
    lt.`rets_2372` AS 'OpenHouseDate2', lt.`rets_2374` AS 'OpenHouseStartTime2', lt.`rets_2376` AS 'OpenHouseEndTime2', lt.`rets_2380` AS 'DisplayOpenHouse2'
    FROM mul_listings AS lt
    WHERE lt.`rets_32` IN ('AGENT1', 'AGENT2') OR lt.`rets_2337` IN ('AGENT1', 'AGENT2') OR lt.`rets_1836` IN ('AGENT1', 'AGENT2'))

    UNION ALL

    (SELECT FORMAT(lt.`rets_111`, 0) AS 'ListPrice', lt.`rets_128` AS 'MlsNumber', lt.`rets_13` AS 'Address', lt.`rets_2044` AS 'PublicRemarks', lt.`rets_sysid`, @real_estate_type:='rnt' AS real_estate_type,
    lt.`rets_2362` AS 'OpenHouseDate1', lt.`rets_2364` AS 'OpenHouseStartTime1', lt.`rets_2366` AS 'OpenHouseEndTime1', lt.`rets_2370` AS 'DisplayOpenHouse1',
    lt.`rets_2372` AS 'OpenHouseDate2', lt.`rets_2374` AS 'OpenHouseStartTime2', lt.`rets_2376` AS 'OpenHouseEndTime2', lt.`rets_2380` AS 'DisplayOpenHouse2'
    FROM rnt_listings AS lt
    WHERE lt.`rets_32` IN ('AGENT1', 'AGENT2') OR lt.`rets_2337` IN ('AGENT1', 'AGENT2') OR lt.`rets_1836` IN ('AGENT1', 'AGENT2'))

    UNION ALL

    (SELECT FORMAT(lt.`rets_111`, 0) AS 'ListPrice', lt.`rets_128` AS 'MlsNumber', lt.`rets_13` AS 'Address', lt.`rets_2044` AS 'PublicRemarks', lt.`rets_sysid`, @real_estate_type:='ici' AS real_estate_type,
    lt.`rets_2362` AS 'OpenHouseDate1', lt.`rets_2364` AS 'OpenHouseStartTime1', lt.`rets_2366` AS 'OpenHouseEndTime1', lt.`rets_2370` AS 'DisplayOpenHouse1',
    lt.`rets_2372` AS 'OpenHouseDate2', lt.`rets_2374` AS 'OpenHouseStartTime2', lt.`rets_2376` AS 'OpenHouseEndTime2', lt.`rets_2380` AS 'DisplayOpenHouse2'
    FROM ici_listings AS lt
    WHERE lt.`rets_32` IN ('AGENT1', 'AGENT2') OR lt.`rets_2337` IN ('AGENT1', 'AGENT2') OR lt.`rets_1836` IN ('AGENT1', 'AGENT2'))

    UNION ALL

    (SELECT FORMAT(lt.`rets_111`, 0) AS 'ListPrice', lt.`rets_128` AS 'MlsNumber', lt.`rets_13` AS 'Address', lt.`rets_2044` AS 'PublicRemarks', lt.`rets_sysid`, @real_estate_type:='far' AS real_estate_type,
    lt.`rets_2362` AS 'OpenHouseDate1', lt.`rets_2364` AS 'OpenHouseStartTime1', lt.`rets_2366` AS 'OpenHouseEndTime1', lt.`rets_2370` AS 'DisplayOpenHouse1',
    lt.`rets_2372` AS 'OpenHouseDate2', lt.`rets_2374` AS 'OpenHouseStartTime2', lt.`rets_2376` AS 'OpenHouseEndTime2', lt.`rets_2380` AS 'DisplayOpenHouse2'
    FROM far_listings AS lt
    WHERE lt.`rets_32` IN ('AGENT1', 'AGENT2') OR lt.`rets_2337` IN ('AGENT1', 'AGENT2') OR lt.`rets_1836` IN ('AGENT1', 'AGENT2'))

    UNION ALL

    (SELECT FORMAT(lt.`rets_111`, 0) AS 'ListPrice', lt.`rets_128` AS 'MlsNumber', lt.`rets_13` AS 'Address', lt.`rets_2044` AS 'PublicRemarks', lt.`rets_sysid`, @real_estate_type:='lot' AS real_estate_type,
    lt.`rets_2362` AS 'OpenHouseDate1', lt.`rets_2364` AS 'OpenHouseStartTime1', lt.`rets_2366` AS 'OpenHouseEndTime1', lt.`rets_2370` AS 'DisplayOpenHouse1',
    lt.`rets_2372` AS 'OpenHouseDate2', lt.`rets_2374` AS 'OpenHouseStartTime2', lt.`rets_2376` AS 'OpenHouseEndTime2', lt.`rets_2380` AS 'DisplayOpenHouse2'
    FROM lot_listings AS lt
    WHERE lt.`rets_32` IN ('AGENT1', 'AGENT2') OR lt.`rets_2337` IN ('AGENT1', 'AGENT2') OR lt.`rets_1836` IN ('AGENT1', 'AGENT2'))

    ORDER BY 'ListPrice' DESC

1 个答案:

答案 0 :(得分:1)

这是您的order by

ORDER BY 'ListPrice' DESC

按常量字符串排序。试试这个:

ORDER BY ListPrice DESC

并且,将来只对字符串和日期常量值使用单引号。永远不要忘记数据库中的东西。