So I want to store searches on my site in a database and I want them to be unique even though the order of words doesn't matter for me for example:
some words
and
words some
are the same for me, and I just want to update the time of the last search.
So is there a way to do this in SQL (like UNIQUE) or should I find a solution in a server side language ?
And thanks in advance.
I don't know if there is a better way to do this, but the only way I can think is that in PHP you split the string by spaces and then order it alphabetically:
$words = explode(" ", $keywords);
sort($words);
$sorted_keywords = implode(" ", $words);
Then when you store/search from database you'll always be using the same string no matter which order the words WERE in.
$words = preg_split("\b", $words);
sort($words);
implode(" ", $words);
You can also just explode on a space if you're definitely separating words by spaces and you can use array_unique
as well. This will be a sorted string listed the searched words.
I seriously doubt if DBMS provides such a facility out of the box. You would need to normalize your search text in such a way that
normalize("some words") = normalize("words some")
and then you can store the normalized version in your database and update it whenever a different search text resulting in the same normalized form is encountered.