0

这是我的查询:

SELECT * From `users` AS `User`
LEFT JOIN `picks` AS `Pick` ON (`Pick`.`User_id` = `User`.`id`)
LEFT JOIN  `owners` AS `owner` ON (`owner`.`id` = `Pick`.`owner_id`)
LEFT JOIN  `owners_seeds` AS `OwnersSeed` ON (`OwnersSeed`.`ownerdate` = (`owner`.`id`)
LEFT JOIN  `labels` AS `Label` ON (`Label`.`id` = `Pick`.`label_id`) WHERE User.market_id = 1111

查询工作正常,但结果如下:

USER X

PICK X

OWNER X

ETC..

由于每个用户都有很多选择,但每个选择只有一个所有者等等,我希望我的结果打印如下:

用户 X

  1. 选择 1
    1. 所有者 1
      1. 所有者种子
    2. 所有者 2
      1. 所有者种子
    3. 业主 3...
  2. 选择 2

ETC

我怎样才能做到这一点?

谢谢

4

1 回答 1

0
SELECT
    users.user,
    picks.pick,
    owners.owner,
    owners_seeds.seed
From
    `users`
    LEFT JOIN `picks` ON (`Picks`.`User_id` = `Users`.`id`)
    LEFT JOIN  `owners` ON (`owners`.`id` = `Picks`.`owner_id`)
    LEFT JOIN  `owners_seeds` ON (`Owners_Seeds`.`ownerdate` = (`owners`.`id`)
    LEFT JOIN  `labels` ON (`Labels`.`id` = `Picks`.`label_id`)
WHERE
    users.market_id = 1111
group by users.user, picks.pick, owners.owner, owners_seeds.seed
order by users.user, picks.pick, owners.owner, owners_seeds.seed;

如果您的架构与您描述的一样,则应输出类似于以下内容的结果:

user     pick     owner    seed
------------------------------------
1        1        x        a
1        2        y        b
1        3        z        c
2        1        x        a
. . .
于 2012-06-08T19:37:12.430 回答