MySQL Left Join返回错误

时间:2012-05-23 00:16:20

标签: mysql left-join

也许我没有在很长时间内完成LEFT JOIN,但我很困惑。

以下是我的表格:

Table: user_phones

user    phone       carrier
----------------------------
danny   5045551234  Verizon
mom     5551234567  T-Mob
tony    5121234567  T-Mob

Table carriers

carrier    email_exchange
--------------------------
Verizon    vtext.com
T-Mob      tmomail.net

这是我的疑问:

SELECT user, CONCAT(phone, '@', email_exchange) AS sms
LEFT JOIN carriers ON (user_phones.carrier = carriers.carrier)
FROM user_phones

我一直回来:

  

'LEFT JOIN运营商ON(user_phones.carrier = carriers.carrier)FROM   user_phones'

我知道这是一个笨蛋疏忽,但变化没有帮助(将ON子句放在括号中等)不起作用。

我在哪里滑倒?

3 个答案:

答案 0 :(得分:3)

您错过了FROM user_phones

SELECT a.`user`, 
       CONCAT(a.`phone`, '@', b.`email_exchange`) AS sms
FROM user_phones a LEFT JOIN carriers b 
        ON a.carrier = b.carrier

答案 1 :(得分:1)

FROM条款放在LEFT JOIN

之前
SELECT `user`, CONCAT(phone, '@', email_exchange) AS sms
FROM user_phones LEFT JOIN carriers ON user_phones.carrier = carriers.carrier

一个小建议是在id表中使用自动增量carriers字段,更新user_phones.carrier以保留id个字段,并加入其中。 HTH

答案 2 :(得分:0)

尝试:

SELECT user, CONCAT(phone, '@', email_exchange) AS sms
FROM user_phones
LEFT JOIN carriers ON (user_phones.carrier = carriers.carrier)

w3schools - Left Join