2012-06-08 134 views
0

這裏是我的查詢:MySQL的簡單的子查詢/加入

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
      1. 所有者種子
    3. 奧尼因爲r 3 ...
  2. 選擇2

我怎麼能這樣做?

感謝

回答

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 
. . .