【发布时间】:2015-09-01 11:14:48
【问题描述】:
我正在尝试加入两张桌子,但最后一张是两次。 我得到的是这样的:
Table 1
id name email
476 Lars Lyngsoe test@test.test
478 Lars2 Lyngsoe2 test2@test2.test2
495 Lars3 Lyngso3 test3@test3.test3
Table 2
user_id profile_key profile_value
476 'profile.klasse' '10A'
495 'profile.klasse' '10B'
476 'profile.phone' '12345678'
478 'profile.klasse' '10A'
478 'profile.phone' '23432123'
495 'profile.phone' '21212143'
其中表 1 中的 id 等于表 2 中的 user_id
我尝试加入并进行子查询,但没有任何效果。 我想要实现的是:
Table
id name email class phone
476 Lars Lyngsoe test@test.test '10A' '12345678'
478 Lars2 Lyngsoe2 test2@test2.test2 '10A' '23432123'
495 Lars3 Lyngso3 test3@test3.test3 '10B' '21212143'
感谢您的帮助。
拉尔斯
【问题讨论】:
-
在问题中包含您尝试过的查询。
-
你的电话号码是从哪里得到的?
-
而不是加入两次反转联接,因此表 2 是您的主表,然后针对 tbl 1 为每个用户 id 进行内部联接,因此每条记录返回多行然后只是 where 子句 tbl1.user_id 与传入的 id它会返回你想要的。