請檢查下面多對多的MySQL棘手的條件
用戶我的表
id name
1 John
2 Peter
3 Predator
手機
id name
1 apple
2 nexus
3 nokva
phones_users
phone_id user_id
1 1
2 1
2 2
3 3
好吧,我想這樣的結果:
phone_id phone_name user_id user_name
1 apple 1 John
2 nexus 1 John
2 nexus 2 Peter
我試過一個請求,但它不工作果然
select `phones`.`name` as `phone_name`, `phones`.`created_at`, `phones`.`id`, `users`.`name` as `user_name`, `users`.`id` as `user_id` from `phones` left join `phones_users` on (`phones`.`id` = `phones_users`.`phone_id` and `phones_users`.`user_id` = 1) left join `users` on `phones_users`.`phone_id` = `phones`.`id` where `users`.`name` is not null;
我想所有具有手機ID = 1的用戶以及所有擁有屬於他的電話的用戶。
例如,John有蘋果和nexus,Peter有nexus,我想用蘋果,nexus,John,Peter 。
THANKS
它的工作原理!非常感謝!你真棒 – 2014-09-07 05:29:42