我有三個表的MySQL查詢有問題。我想搜索一個名字,並獲得所有(甚至更好,只有第一個)電話號碼和電子郵件。這裏是我的表:MySQL:從三個表中選擇?
Table 1, contact
==========
id | name
==========
1 | stefan
2 | michael
3 | andy
4 | bob
Table 2, phone
==============================
id | contact_id | phonenumber
==============================
1 | 1 | +1 434 434232
2 | 1 | +1 434 24234
3 | 2 | +1 89234
4 | 4 | +1 345345
5 | 4 | +1 434 7567567
Table 3, email
===============================
id | contact_id | emailaddress
===============================
1 | 1 | [email protected]
2 | 1 | [email protected]
3 | 1 | [email protected]
4 | 4 | [email protected]
5 | 2 | [email protected]
,這是我的查詢,其接縫送MySQL來必殺技:
SELECT c.name, p.phonenumber, e.emailaddress
FROM contact AS c
JOIN phonenumber AS p ON c.id = p.contact_id
JOIN email AS e ON c.id = e.contact_id
WHERE c.name = 'michael'
當我只有一個加入這個工作正常,如:
SELECT c.name, p.phonenumber
FROM contact AS c
JOIN phonenumber AS p ON c.id = p.contact_id
WHERE c.name = 'michael'
有什麼建議嗎?
感謝
邁克
您別名電子郵件爲 'A',但你在e.contact_id中使用'e'。 –
對不起,這是一個錯字,我改變了它,但總體上存在問題。 – MichaelFromMars
你是什麼意思「發送MySQL到涅」「?你有錯誤嗎?是否需要永久執行?你沒有得到任何結果?請具體說明。 –