-1

I have multiple tables each connected by PersonID.

  • PhoneNumber contains columns for phone number type (work, cell, home), phone numbers, a value for whether SMS messaging is enabled, and person IDs.
  • Person contains names and email addresses and its key is person ID
  • AttributeValue contains the values for a number of 'custom' attributes (each attribute has a unique 'AttributeId'). The key columns in this table are AttributeId, Value, and EntityId. EntityId matches Person Id

I'm trying to find the corresponding names and email addresses from the PersonTable for the Person ID that meets the following criteria.

FROM [rock].[dbo].[AttributeValue]
WHERE AttributeId='1770';

FROM [rock].[dbo].[PhoneNumber]
WHERE IsMessagingEnabled=0 AND NumberTypeValueId=12
OldAccount
  • 51
  • 1
  • 6
  • Yes, it is possible. in fact, [there are many ways you can do it.](http://stackoverflow.com/a/29362848/3094533) However, in your case I think what you are looking for is a JOIN, where you get data from multiple tables. – Zohar Peled Apr 27 '15 at 14:52
  • Which DBMS are you using? Postgres? Oracle? –  Apr 27 '15 at 16:56
  • Please share your table structures, some sample data, and the result you're trying to get for that data. – Mureinik Apr 30 '15 at 06:43
  • Sorry for the slow reply guys. I've been caught up with lots of other projects. I'm using MS SQL. I have altered my original question because I actually need to look against three tables. – OldAccount May 02 '15 at 09:08

2 Answers2

1

First of all in SQL when you got multiple tables related by one ID you can use joins.

For example I got a table Customer(ID,Name,Firstname) , a table Address(ID, CustomerID, Name, PostalCode, City) and a table Phone(ID,CustomerID,PhoneHome,PhoneMobile)

You can use this kind of query to find addresses and phones corresponding to customers :

SELECT [Address.Name], [Phone.PhoneHome], [Phone.PhoneMobile] FROM Address, Phone
INNER JOIN Address ON [Customer.ID] = [Address.CustomerID]
INNER JOIN Phone ON [Customer.ID] = [Phone.CustomerID]
WHERE [Customer.ID] = "Value"

Hope it will help you, at least you can find good examples of joints on :

http://openclassrooms.com/courses/introduction-aux-jointures-sql (I believe you're french so this is a good french site to learn about it.)

Have a good day

VERYNET
  • 531
  • 2
  • 14
0

Something like this?

SELECT * FROM Person WHERE PersonID IN (SELECT PersonID FROM ....)
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193