mysql联合都不起作用

Posted

技术标签:

【中文标题】mysql联合都不起作用【英文标题】:mysql union all not working 【发布时间】:2014-02-11 21:54:01 【问题描述】:

使用下面的 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】:

这是你的order by

ORDER BY 'ListPrice' DESC

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

ORDER BY ListPrice DESC

而且,在未来,字符串和日期常量值只能使用单引号。永远不会,永远不会用于数据库中事物的名称。

【讨论】:

谢谢!抱歉,如果这是一个愚蠢的问题,但是您在代码中指的是关于单引号以及应如何使用它们的代码? @user3299111 。 . .单引号是出现在ListPrice 周围的'。这会导致列名被视为常量值,这对于排序没有用处。 所以如果名称中没有空格,我不需要任何形式的引号? 如果名称不是 SQL 中的保留字(如“like”、“in”、“select”、“from”、“between”等)并且您只使用字母数字字符、数字和下划线,则不需要引用变量名。 如果你不介意还有一个问题,我使用的反引号呢?在使用保留字的情况下,作为预防措施和习惯是必要的吗?

以上是关于mysql联合都不起作用的主要内容,如果未能解决你的问题,请参考以下文章

BigQuery - 联合上的相关子查询不起作用

使用 ctypes 的联合操作不起作用

“导入”和“要求”都不起作用

联盟都不起作用

fileExistsAtPath 和 checkResourceIsReachableAndReturnError 都不起作用

更新到 Android Studio 4.2 后,查找和替换都不起作用