[英]How to create a contacts page properly?
我想做一個聯系頁面。 這是我的表結構:
Accounts Table:
1) id
2) User_id
3) Full_name
4) Username
5) Email
6) Password
Contacts Table:
1) My_id (This is the user who added friend_id)
2) Contact_id (this is the contact who was added by my_id)
3) Status
我的查詢看起來像這樣:
$sid = ID of user who is viewing;
$sql = "SELECT STRAIGHT_JOIN DISTINCT contacts.contact_id,
accounts.full_name FROM contacts INNER JOIN accounts
on contacts.contact_id = accounts.user_id WHERE
contacts.my_id = '$sid' OR contacts.contact_id = '$sid'";
問題是它不能以正確的方式工作。 我最終在查詢中看到了我的名字(意思是當我登錄時,我在聯系人中看到我的名字而不是聯系人名稱)。
如何解決這個問題? 謝謝。
這里不需要STRAIGHT_JOIN
關鍵字。 要獲取聯系信息,請使用第二個JOIN
到contacts
表,該contacts
表將其與帳戶相關聯。
SELECT
DISTINCT c.contact_id,
cn.full_name
FROM
accounts a
/* first join connects the adding user's id to records in contacts */
INNER JOIN contacts c ON a.user_id = c.My_id
/* second join connects contact list user ids back to names in accounts */
INNER JOIN accounts cn ON cn.user_id = c.Contact_id
WHERE a.User_id = '$sid'
這個查詢應該足夠了:
SELECT DISTINCT contacts.contact_id, accounts.full_name
FROM contacts, accounts
WHERE (contacts.my_id = '$sid' AND contacts.contact_id = accounts.user_id)
OR (contacts.contact_id = '$sid' AND contacts.my_id = accounts.user_id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.