0

我有以下查询来提取属性。属性有多个单位。我不知道如何使用至少 1 个单位提取属性。单元通过具有相同的“团队”连接到属性。“团队”是一个 varchar(例如“jkdgsl”)。有什么建议么?

SELECT DISTINCT Properties.id, Properties.title, Properties.team
FROM Properties
INNER JOIN Units ON Units.team = Properties.team 
AND Units.rates != '0' 
AND Properties.deleted =  '0000-00-00 00:00:00'
AND Properties.zip_code
IN (
'77808',  '77845'
)
AND Properties.public =  '1'
ORDER BY FIELD( Properties.zip_code,  '77808',  '77845' ) 
LIMIT 0 , 30
4

3 回答 3

0

我认为您已经通过内部连接的性质这样做(即选择具有至少一个单元的属性)。

于 2012-07-09T19:23:58.297 回答
0

我同意@Razvan 如果您还想要单位数,请使用:

SELECT DISTINCT Properties.id, Properties.title, Properties.team, COUNT(Units.<em>someOTHERcolumn</em>)
FROM Properties
INNER JOIN Units ON Units.team = Properties.team 
AND Units.rates != '0' 
AND Properties.deleted =  '0000-00-00 00:00:00'
AND Properties.zip_code
IN (
'77808',  '77845'
)
AND Properties.public =  '1'
GROUP BY Properties.id ORDER BY FIELD( Properties.zip_code,  '77808',  '77845' ) 
LIMIT 0 , 30

于 2012-07-09T19:28:52.717 回答
0

如果只是一个或多个,您可以使用COALESCE.

WHERE COALESCE( Properties.id, Properties.title, Properties.team ) IS NOT NULL
于 2012-07-09T19:34:00.177 回答