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


Delete rows based on a second table


Delete rows based on a second table

Author
Message
xxx-593414
xxx-593414
Mr or Mrs. 500
Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)

Group: General Forum Members
Points: 579 Visits: 62
I have two Tables (In Ms Sql Server)
Table Name Column Names
MasterCities Countryy City
VisitedCities Country City

I want to delete from the MasterCities table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables
So I want to end up , in the MasterCities table, all the Countries/Cities that originally exist in the Master Table, except those that are in the VisitedCities
Have tried various selects where not in etc. but cannot get syntax right or its deleting too much stuff
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221344 Visits: 25147
xxx-593414 - Monday, February 19, 2018 8:02 AM
I have two Tables (In Ms Sql Server)
Table Name Column Names
MasterCities Countryy City
VisitedCities Country City

I want to delete from the MasterCities table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables
So I want to end up , in the MasterCities table, all the Countries/Cities that originally exist in the Master Table, except those that are in the VisitedCities
Have tried various selects where not in etc. but cannot get syntax right or its deleting too much stuff

Something like this (untested)
delete mc
from MasterCities mc
where exists (select 1 from VisitedCities vc where vc.Country = mc.Country and vc.City = mc.City)



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Thomas Rushton
Thomas Rushton
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: Moderators
Points: 19995 Visits: 10522
It sounds as though you want the "DELETE FROM x JOIN y" type of query shown in the example D of the DELETE syntax - https://docs.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql#d-using-joins-and-subqueries-to-data-in-one-table-to-delete-rows-in-another-table

For example:

--table variables because lazy
DECLARE @MasterCities TABLE (Country varchar(20), city varchar(20))
DECLARE @VisitedCities table (Country varchar(20), city varchar(20))

--dummy data
INSERT INTO @MasterCities values('UK', 'London'), ('UK', 'Birmingham'), ('UK','Edinburgh'), ('UK','Belfast'), ('Canada', 'London')
INSERT INTO @VisitedCities VALUES ('UK', 'London'), ('UK', 'Edinburgh')

--see what we've got so far
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities

--delete matching records from Master table
DELETE m
FROM
@MasterCities AS m
INNER JOIN @VisitedCities AS v ON v.Country = m.Country
AND v.city = m.city;

--check the results
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities

xxx-593414
xxx-593414
Mr or Mrs. 500
Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)

Group: General Forum Members
Points: 579 Visits: 62
Phil Parkin - Monday, February 19, 2018 8:23 AM
xxx-593414 - Monday, February 19, 2018 8:02 AM
I have two Tables (In Ms Sql Server)
Table Name Column Names
MasterCities Countryy City
VisitedCities Country City

I want to delete from the MasterCities table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables
So I want to end up , in the MasterCities table, all the Countries/Cities that originally exist in the Master Table, except those that are in the VisitedCities
Have tried various selects where not in etc. but cannot get syntax right or its deleting too much stuff

Something like this (untested)
delete mc
from MasterCities mc
where exists (select 1 from VisitedCities vc where vc.Country = mc.Country and vc.City = mc.City)


That's perfect and worked fine
Many thanks
xxx-593414
xxx-593414
Mr or Mrs. 500
Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)

Group: General Forum Members
Points: 579 Visits: 62
ThomasRushton - Monday, February 19, 2018 8:25 AM
It sounds as though you want the "DELETE FROM x JOIN y" type of query shown in the example D of the DELETE syntax - https://docs.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql#d-using-joins-and-subqueries-to-data-in-one-table-to-delete-rows-in-another-table

For example:

--table variables because lazy
DECLARE @MasterCities TABLE (Country varchar(20), city varchar(20))
DECLARE @VisitedCities table (Country varchar(20), city varchar(20))

--dummy data
INSERT INTO @MasterCities values('UK', 'London'), ('UK', 'Birmingham'), ('UK','Edinburgh'), ('UK','Belfast'), ('Canada', 'London')
INSERT INTO @VisitedCities VALUES ('UK', 'London'), ('UK', 'Edinburgh')

--see what we've got so far
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities

--delete matching records from Master table
DELETE m
FROM
@MasterCities AS m
INNER JOIN @VisitedCities AS v ON v.Country = m.Country
AND v.city = m.city;

--check the results
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities


Many thanks
Joe Celko
Joe Celko
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7294 Visits: 3927
xxx-593414 - Monday, February 19, 2018 10:17 AM
[quote]
ThomasRushton - Monday, February 19, 2018 8:25 AM



>> I have two Tables (In Ms Sql Server) <<

Where is the DDL for these alleged tables? Why doesn't your narrative. Follow any ISO 11179 naming rules? Now we have to do everything for you, because you can bother to read even the front end of this form before posting. Are you always this rude to people that you're asking to help you for free?

Then you screwed up at a more fundamental level. The metadata term. "Master" was derived from tape filesystems in the 1950s. A master tape was a magnetic tape file that held the most current information, this terminology was then carried over in network database systems. It has absolutely no place in RDBMS. Next you confused attributes and entities. Being visited is not a whole separate kind of entity. It is a status (state of being) or attribute of the city.

CREATE TABLE Cities
(country_code CHAR(3) NOT NULL, -- look up ISO country code
city_name VARCHAR(20) NOT NULL,
PRIMARY KEY (country_code, city_name),
visitation_count INTEGER NOT NULL DEFAULT 0 CHECK (visitation_status I>= 0));

>> I want to delete from the MasterCities [sic] table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables <<

Every table to model a distinct and totally different set of entities. You visiting a city does not change its very nature any more than your sitting in an automobile would turn it into a squid. What you're doing is mimicking the way we would have done this with decks of punch cards in the 1950s.

In some destroying information, let's count the number of times you visited a particular city. Obviously that's going to be zero or more visits. And you'll do it with an update statement

Please post DDL and follow ANSI/ISO standards when asking for help.
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