Reputation: 167
Is there an SQL statement to check if a field exists?
Example:
I have a database with the following: URL, IsCrawled, Level
Etc.
I need to check to see if the URL is already there.
I do not care about the other fields.
I am writing a very fast crawler, and this is needed.
Upvotes: 2
Views: 250
Reputation: 15557
Why don't you use a plain select under any index:
SELECT ID FROM TABLE WHERE URL = @URL
If the select isn't null then there's already one.
Now, another better idea depending on your problem would be to add a UNIQUE CONSTRAINT so by design your DB will not allow duplicates of that value.
Now, going a little forward if you want to compare only part of the URL to see (ie the domain) then you can use the LIKE comparison function to see if there's already one URL stored with the same domain name.
Upvotes: 1
Reputation: 263723
Is this what you want?
SELECT COUNT(*)
FROM tableName
WHERE `URL` = 'urlHERE'
It will have a result of 0
if it doesn't exists. Or add a unique index on the fieldd URL
ALTER TABLE tableName
ADD CONSTRAINT indexName UNIQUE (`Url`)
in this way, the Insert
statement will fail if you insert a url which already exists.
Upvotes: 1
Reputation: 80639
ALTER TABLE `table`
ADD UNIQUE INDEX `urls` (`URL`)
or simple,
ALTER TABLE `table`
ADD UNIQUE(`URL`);
Though, I use the first method. Have only read about the second method to work too. It may not be so.
Upvotes: 3