我有以下查询来提取属性。属性有多个单元。我不知道如何拉至少一个单位的属性。单位通过拥有相同的“团队”连接到属性。 “团队”是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
答案 0 :(得分:0)
我认为你已经按照内部联接的性质这样做了(即选择至少有一个单位的属性)。
答案 1 :(得分: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
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
答案 2 :(得分:0)
如果它只是一个或多个,您可以使用COALESCE
。
WHERE COALESCE( Properties.id, Properties.title, Properties.team ) IS NOT NULL