开发者

SQL query compare and get info based on two tables

开发者 https://www.devze.com 2023-04-06 21:55 出处:网络
I have two tables. Contacts and groupcontacts. I want all contacts from group x listed. Table A Tablename : contacts

I have two tables. Contacts and groupcontacts. I want all contacts from group x listed.

Table A Tablename : contacts Fields : crmcListId crmcId

Table B Tablename: contactgroups Fields : crmcgrId crmcgrContactId crmcgrGroupId

Fiel开发者_运维技巧d crmcgrID = crmListID Field crmcID = crmContactID

I want all contacts from table a belonging to group x to list. From table contactgroups where crmcgrGroupID = x


Do something like :

SELECT c.* FROM Contacts c WHERE c.ID IN (SELECT ContactID FROM groupcontacts) 

OR with a join :

SELECT c.* FROM Contacts c 
INNER JOIN groupcontacts gc ON c.ID = gc.ContactID

You can then add a WHERE clause to select the IDs for a specific group.


Since it's not entirely clear what your tables look like, I'm going to take a shot in the dark here:

Select * 
From Contacts c
inner join GroupContacts gc on gc.ContactID = c.ContactID
where gc.GroupID = x

Just subsitiute x for the real group id

0

精彩评论

暂无评论...
验证码 换一张
取 消