选择不会在映射表中出现的值

时间:2016-04-05 10:57:00

标签: mysql sql

我有3张桌子:

================
| contacts     |
================
| id | name    |
================

================
| contact_map  |
================
| cid | lid    |
================

================
| contact_list |
================
| id | name    |
================

我需要查找未分配给列表的所有联系人。 cidcontact_map id contacts lid contact_map id contact_list lid {/ 1}}。{ p>

选择lid的联系人相对容易,但我无法弄清楚如何选择没有public void initFB() { if (!FB.IsInitialized) { FB.Init (setInit, onUnityHide); } else { isLoggedIn = FB.IsLoggedIn; } } public void setInit() { if (FB.IsLoggedIn) { Debug.Log ("u r logged in!"); getProfile(); } else { Debug.Log ("u r not logged in!"); } isLoggedIn = FB.IsLoggedIn; } private void onUnityHide(bool isShown) { if (!isShown) { Time.timeScale = 0; } else { Time.timeScale = 1; } } public void FBLogin() { List<string> perms = new List<string> (); perms.Add ("public_profile"); FB.LogInWithReadPermissions (perms, authCallBack); } public void authCallBack(IResult result) { if (result.Error != null) { Debug.Log (result.Error.ToString ()); } else { if (FB.IsLoggedIn) { FacebookManager.Instance.isLoggedIn = true; FacebookManager.Instance.getProfile(); Debug.Log ("you are logged in!"); } else { Debug.Log ("you are not logged in!"); } setMenu(FB.IsLoggedIn); } } public void share() { FB.FeedShare ( string.Empty, null, "Hi Title!", "Hi Caption!", "Hi Description!", new System.Uri("http://s3.img7.ir/hPyQA.jpg"), string.Empty, shareCall ); } public void shareCall(IResult result) { if (result.Cancelled) { Debug.Log ("share cancelled!"); } else if (!string.IsNullOrEmpty (result.Error)) { Debug.Log ("error on share!"); } else if (!string.IsNullOrEmpty (result.RawResult)){ Debug.Log ("success on share!"); } } 的人。

任何帮助?

2 个答案:

答案 0 :(得分:1)

您可以使用not existsnot in

select c.*
from contacts c
where not exists (select 1
                  from contact_map cm
                  where cm.cid = c.id
                 );

如果您希望联系人未分配到特定列表,则只需在子查询中包含该信息:

select c.*
from contacts c
where not exists (select 1
                  from contact_map cm
                  where cm.cid = c.id and cm.lid = $lid
                 );

答案 1 :(得分:1)

我认为您实际上不需要使用contact_list表,因为contact_map表允许您确定哪些联系人已添加到列表中。作为Gordon答案的替代方案,您可以LEFT JOINcontacts表格添加到contact_map并保留所有映射到任何内容的记录。

SELECT c.*
FROM contacts c LEFT JOIN contact_map cm
    ON c.id = cm.cid
WHERE cm.cid IS NULL