MySql简单子查询/加入

时间:2012-06-08 19:29:13

标签: mysql database

这是我的问题:

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

等。

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

  

USER X

     
      
  1. PICK 1   
        
    1. 所有者1   
          
      1. 所有者种子
      2.   
    2.   
    3. 所有者2   
          
      1. 所有者种子
      2.   
    4.   
    5. 所有者3 ......
    6.   
  2.   
  3. 选择2
  4.   

我该怎么做?

由于

1 个答案:

答案 0 :(得分: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
. . .