如何使用3表联接zend / sql获取对象列表?

时间:2019-04-12 18:08:35

标签: php sql zend-framework zend-db

我有三个桌子。

  • Football_club

  • Football_club_player

  • Football_club_Title

最后两个表是多对一的 与桌子Football_club的关系

表的结构:

Football club
coulmns:

football_club_id
name
...

_______________________________

Football club Player
coulmns:

football_club_player_id
football_club_id
...

________________________

Football_club_Title
coulmns:

Football_club_title_id
football_club_id
...

我想列出一个足球俱乐部。此列表必须满足以下要求: -此列表中不应有以下足球俱乐部:一人正好&&零俱乐部头衔。

Iam正在使用zend db select。我要接收的列表应该是Model_Football_Clubs的数组。

如何使用zend或通常使用sql实现此目的?

先谢谢了。

1 个答案:

答案 0 :(得分:0)

这是一般的sql。

WITH
    more_than_one_player_clubs AS
    (
        SELECT
            plyr.football_club_player_id,
            COUNT(plyr.name)                    AS players,
            COUNT(title.Football_club_title_id) AS titles
        FROM
            Football_club_player plyr
        LEFT OUTER JOIN
            Football_club_Title title
        ON
            (
                plyr.football_club_id = title.football_club_id )
        GROUP BY
            plyr.football_club_player_id
    )
SELECT
    clb.football_club_id,
    clb.name,
    plyr.football_club_player_id,
    title.Football_club_title_id
FROM
    Football_club clb
LEFT OUTER JOIN
    more_than_one_player_clubs plyr
ON
    (
        clb.football_club_id = plyr.football_club_id )
WHERE
    (
        plyr.titles > 0
    OR  plyr.player > 1 )
OR (
        plyr.titles = 0
    AND plyr.player = 0 )