如果JOIN值大于X,则将value设置为true

时间:2014-06-07 14:09:44

标签: mysql sql

SELECT X(Coordinates) AS `latitude`, Y(Coordinates) AS `longitude`, `AtcoCode` AS `atcocode`, `CommonName` as `name`,
(
    SELECT COUNT(`jptl`.`id`)
    FROM `a`.`jps` `jptl`
    JOIN `a`.`journpat` `jp` ON `jp`.`journey_pattern_section_reference` = `jptl`.`journey_pattern_section_reference`
    JOIN `a`.`serv` ON `service`.`reference` = `jp`.`service_reference`
    JOIN `a`.`op` ON `operator`.`reference` = `service`.`operator_reference`
    JOIN `a`.`operator` `fmbo` ON fmbo.operator_reference = operator.reference
    WHERE `jptl`.`stop_from` = AtcoCode
) AS subscriber
FROM `a`.`qwerty`
WHERE MBRContains(
    GeomFromText( concat('LINESTRING(',50.922538,-1.301773,',',50.916856,-1.306708,')') ),
    Coordinates)

如果subscriber的值大于1,则subscriber值应为true,否则应为false。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您可以使用IF

 ... as `name`, IF((... your query here) > 1, true, false) AS `subscriber`...

检查documentation

另一个选项是修改子查询,并将count(*)与1:

进行比较

... SELECT COUNT(jptlid)> 1 ...

但是原始查询将返回1或0,但大多数ORM会将其解释为TRUEFALSE(如果您使用的话)