SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Filter Countries, States and Cities which in same row in one table.


Filter Countries, States and Cities which in same row in one table.

Author
Message
Sean Pearce
Sean Pearce
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3990 Visits: 3436
Jeff Moden (10/8/2013)
Bhuvnesh (10/8/2013)
ChrisM@Work (10/8/2013)

SELECT * FROM TABLE WHERE countryid = @country
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid AND Cityid = @Cityid



totally agree with you Chris.. but i think here OR operator will be more reliable because with correct data
OR and AND will return same records ..What do you think ?


Gosh, I wouldn't rely on that nuance especially for the sake of clarity. I would absolutely use AND in this case just to avoid any confusion in the heat of an emergency fix, should it ever occur. A newbie wouldn't understand the ORs are an implied AND simply because of the IDs involved.

Can you please explain how the OR is an implied AND. I don't see OR & AND returning the same records.

CREATE TABLE MyTable
(UserID INT,
CityID INT,
StateID INT,
CountryID INT);

-- Two users, in the same state and country but different cities
INSERT INTO MyTable VALUES (1, 1, 1, 1);
INSERT INTO MyTable VALUES (2, 2, 1, 1);

SELECT
*
FROM
MyTable
WHERE
CityID = 1
OR StateID = 1
OR CountryID = 1;

SELECT
*
FROM
MyTable
WHERE
CityID = 1
AND StateID = 1
AND CountryID = 1;

DROP TABLE MyTable;





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search