tags:

views:

84

answers:

6

I have a table with multiple fields for address, such as address_line_1, address_line_2, etc.

Is there any way that I can use a WHERE clause across multiple fields at once, without having to use an OR/AND statement?

For example:

SELECT * 
  FROM FIN_LIVE.CUSTOMER_ADDRESSES 
 WHERE SYS_ADDRESS_1, SYS_ADDRESS_2, SYS_ADDRESS_3 = 'data'
+1  A: 

No, you'll need to use AND/OR.

SELECT * 
FROM FIN_LIVE.CUSTOMER_ADDRESSES 
WHERE SYS_ADDRESS_1 = 'data'
    AND SYS_ADDRESS_2 = 'data'
    AND SYS_ADDRESS_3 = 'data'
Alison R.
That would mean that the same value would have to be present in all three columns. Depends on the data, but line addresses are rarely duplicated.
OMG Ponies
this will only work if you have a bizarre address.
runrunraygun
Yes, but what else could the OP be implying with that clause?
Alison R.
He could be implying an OR condition. So for example, if someone is at 123 Maple Drive, that might be in line 1, line 2, or line 3 of the address depending on how they entered it.
Tom H.
Ahh, true. So it's like a game of database column whack-a-mole. ;)Answer edited to include the possibility of "OR." At any rate, we've got an XY problem here. http://www.perlmonks.org/index.pl?node_id=542341
Alison R.
+1  A: 

No, there is not.

svinto
+5  A: 

Pretty sure you'll have to use AND/OR

HOWEVER, maybe this is a sign to change your data structure. Are you saying that there are 3 addresses, and any one of them could be 123 Main Street?

In that case, perhaps you need to pull address data out to a separate table.

taylonr
Agreed, this sounds like a strange situation that probably should be recognized as a code smell, or a schema smell, if you wish.
Alison R.
@Alison R.: It's common to have at least three address lines for a mailing address. The first line could be Rural Route (RR), or a "care of" (noted as c/o), second could be apt/suite number if separate from the street address. There's no consistent format - dunno if any of the above applies to outside of Canada/US.
OMG Ponies
Correct. But based on the initial query, he's asking if any of the three fields contain 'data' I took that mean, do any of the lines contain '123 Main Street' Our addresses in our internal DB have line 1 for street #, line 2 for anything you mentioned there. But we don't keep a 'home address' 'work address' and 'school address' that's what I wanted to make sure wasn't happening here.
taylonr
OMG Ponies has it right. There are 4 lines we provide for an address, and the format is not consistent. Sometimes a customer's line 1 will be the company name, or a subdivision, or a contact's name and sometimes it will be the exact address.
Brandon
Ah, in that case then, ignore my comment.
taylonr
+1  A: 

In MySQL with MyISAM, you can create a FULLTEXT index

CREATE FULLTEXT INDEX fx_customeraddresses_123 ON FIN_LIVE.CUSTOMER_ADDRESSES (SYS_ADDRESS_1, SYS_ADDRESS_2, SYS_ADDRESS_3)

and issue this query:

SELECT  *
FROM    FIN_LIVE.CUSTOMER_ADDRESSES
WHERE   MATCH(SYS_ADDRESS_1, SYS_ADDRESS_2, SYS_ADDRESS_3) AGAINST ('+data')

, which will return all records with the word data in any of the fields.

You can even query it without the index:

SELECT  *
FROM    FIN_LIVE.CUSTOMER_ADDRESSES
WHERE   MATCH(SYS_ADDRESS_1, SYS_ADDRESS_2, SYS_ADDRESS_3) AGAINST ('+data' IN BOOLEAN MODE)

, but this will be much slower.

If you are looking for exact match in any of three fields, you may use this syntax:

SELECT  *
FROM    FIN_LIVE.CUSTOMER_ADDRESSES
WHERE   'data' IN (SYS_ADDRESS_1, SYS_ADDRESS_2, SYS_ADDRESS_3)

(works in all major databases).

Quassnoi
A: 

If you want to avoid multiple AND statements to only enter the criterion, i.e. SYS_ADDRESS_1 = 'data', once, you could always do:

SELECT * 
  FROM FIN_LIVE.CUSTOMER_ADDRESSES 
 WHERE 
    SYS_ADDRESS_1 = SYS_ADDRESS_2 
    AND SYS_ADDRESS_2 = SYS_ADDRESS_3 
    AND SYS_ADDRESS_3 = 'data'

I'm only pondering why you would want to exclude something as elementary as an AND operator? I don't think I'm alone in that!

Joe Lloyd
A: 

If you're looking for a row where any one of the columns match then you could do something like this, but it would be horribly inefficient compared to the simple OR statements.

SELECT
     *
FROM
     Customer_Addresses
WHERE
     '|*|' + sys_address_1 + '|*|' + sys_address_2 + '|*|' + sys_address_3 + '|*|' LIKE '%|*|' + @search_string + '|*|%'

I'll leave it as an exercise to the reader to turn this into an AND statement :)

Tom H.