PDA

View Full Version : selecting data from 2 tables


crazygamer
12-02-2005, 09:04 AM
I have two tables in a database (MS SQL, but close enough). One table, called Table1 has a field BillingAddress. The other, Table2, has a field ShippingAddress.

Is there any way to search both tables for either a BillingAddress or ShippingAddress which starts with something (say, WHERE field LIKE '1 Street%'), then weed out the duplicates and return them all as one field?

Thanks

pramsey
12-02-2005, 03:01 PM
You need a join statement to pull the values. As far as unique values, try using DISTINCT and see if it works. Hope this helps.

SELECT x.columnName, y.columnName
FROM tableName x, tableName y
WHERE x.columnName = y.columnName

crazygamer
12-02-2005, 04:43 PM
This is almost what I need. However, instead of searching where one field from the first table equals another field from the second table, I need to search if either of them is LIKE a set query (for example LIKE '%dr.' for anyone who's billing OR shipping address has a "dr." in it).

Any ideas?

crazygamer
12-02-2005, 05:01 PM
I just stumbled on UNION and it works for doing this:

SELECT Field1 AS newfield FROM Table1 WHERE Field1 LIKE '%whatever%'
UNION
SELECT Filed2 AS newfield FROM Table2 WHERE Field2 LIKE '%whatever%'