MySQL SELECT不同于单列而不是整个查询

时间:2013-11-17 23:38:14

标签: mysql

在此solution上,用户正在查询id以显示select中的其他列,但查询仅在单个列上不同。我试过这样做,但无法正确使用语法。这是查询,但我只想在DevelopmentDisplay列上进行区分。

SELECT DISTINCT
`RESI`.`DevelopmentDisplay`,
`RESI`.`City`,
`RESI`.`ZipCode`
FROM
`RESI`
WHERE
`RESI`.`ZipCode` =  '91263'
ORDER BY
`RESI`.`DevelopmentDisplay` ASC

1 个答案:

答案 0 :(得分:1)

是的,您可以自行加入表格。

我看到两个这样的选项。但是,我建议您索引DevelopmentDisplay列。根据记录的数量,它可能变得非常慢。

SELECT 
    t1.DevelopmentDisplay,
    t1.City,
    t1.ZipCode
FROM
    RESI t1,
    (SELECT DISTINCT DevelopmentDisplay FROM RESI) t2

WHERE
    t1.ZipCode =  '91263' AND
    t1.DevelopmentDisplay = t2.DevelopmentDisplay;

可替换地:

SELECT 
    t1.DevelopmentDisplay,
    t1.City,
    t1.ZipCode
FROM
    RESI t1,
WHERE
    t1.ZipCode =  '91263' AND
    t1.DevelopmentDisplay IN (SELECT DISTINCT DevelopmentDisplay FROM RESI);