You have 2 columns Link, Status in the db MyDatabase.
Link column contains strings, the values look like http://anysite.com/some/more/symbols?... (varchar(256)).
Status values could be NULL (by default) or 0 or 1 or 2. (int(2)).
How to update column Status so that NULL will be replaced by 0 or 1 or 2 using this rules:
0) we split Link column by groups depending on its value (links that follow to the same web-site, even to the different pages, are in the same group)
1) for all links in the same group we replace NULL with 0, if Status 0 is at least for 1 link within that group;
for all links in the same group we replace NULL with 1, if Status 1 is at least for 1 link within that group and there are no zeroes;
for all links in the same group we replace NULL with 2, if Status 2 is at least for 1 link within that group and there are no 0 or 1;
So that 0 has a highest priority, then goes 1, then 2, and NULL has the lowest priority.
2) if all Status are NULL for 1 group, we do nothing.
As a result, the database MyDatabase
Link Status
-------------------------------------- ------
http://stackoverflow.com/question NULL
http://google.com/?query=AmIPregnant 2
http://stackoverflow.com/question/ask NULL
http://google.com/?query=weather NULL
http://google.com/?query=love 1
http://stackoverflow.com/question/abcd NULL
should become:
Link Status
-------------------------------------- ------
http://stackoverflow.com/question NULL
http://google.com/?query=AmIPregnant 1
http://stackoverflow.com/question/ask NULL
http://google.com/?query=weather 1
http://google.com/?query=love 1
http://stackoverflow.com/question/abcd NULL
All links contain http://, no https://, no ftp:// are in .com and have at least / symbols after .com (this is to simplify the question about this query).
Is it possible to make using just 1 query?
To better understand the question, just guess 1= “I have checked, this web-site contains AT LEAST 1 page in English”, 2= “I have checked, this web-site contains AT LEAST 1 page in French”, NULL(by default) actually means “I visit 0 pages at this web-site”.
The goal is to update the database using just 1 query.
I would guess SELECT Link as currentlink, Status AS currentstatus FROM MyDatabase WHERE Status is NOT NULL could be used to select those values that are not null to update the rest of values, but then I’m stuck.
Thank you.
why dont you use an UDF (store procedure) to do that programatically?
Well, you can try to do it with some UPDATE with sub strings and IF/HAVING structures, but that will be more time expensive. The real way to do that is with a temporal table, or a custom cursor to fetch the rows. Maybe, as the last option, you can find a work around script to do that in a schedule time or with a trigger (when something is affected by… event).
Anyway, please me say it to you, the way that you re looking for is more complex and time expensive that the right way to do it.