选择将多行连接成单行

时间:2014-01-08 23:31:30

标签: mysql select left-join

我有一张这样的表

用户

name   |  phone   |  email
jane   | 123456   |jane@test.com
jack   | 223456   |jack @test.com

我想选择这两个用户给出的电话号码是唯一的,请给出

borrower  |  lender   |  email
name      |  jack     | jane@test.com

这是我试过的

SELECT  u1.name borrower,u1.email, u2.name lender  FROM users u1  
LEFT JOIN users u2 on u2.phone='123456' WHERE u1.phone='223456'

这给了我4行而不是一行可能出错?

1 个答案:

答案 0 :(得分:1)

参见 SQLFIDDLE

SELECT 
  u1.name borrower,
  u1.email, 
  u2.name lender 
FROM 
  users u1, 
  users u2 
WHERE 
  u2.phone='123456' 
  and 
  u1.phone='223456'
;

enter image description here