Reputation: 21
Hello i have a string that is stored in my database separated by comma eg: (new south wales,Queensland,etc,etc) Know my problem is when i try to search Queensland i am not able to get the result but when i try to search for new south wales i get the record.
But i want to get the result when i try to search for queen or etc. I am new to php so please help...
Upvotes: 2
Views: 2468
Reputation: 25053
Not really what you were asking, but just to be complete: you're going to have a lot of trouble unless you change your approach.
The correct way:
TableOne
--------
ThingID
TableTwo
--------
ThingID
Province
Then your database query becomes:
SELECT fields FROM TableOne WHERE ThingID IN
(SELECT ThingID from TableTwo WHERE Province = 'Queensland')
And what do you want to have happen when they search for "Australia"? Get back both Western Australia and South Australia?
Upvotes: 3
Reputation: 332571
Use the FIND_IN_SET function:
WHERE FIND_IN_SET('Queensland', csv_column)
...because using LIKE with wildcards on either end is risky, depending on how much/little matches (and it also ensures a table scan). Performance of LIKE with wildcards on either side is on par with REGEXP--that means bad.
Don't store comma separated values -- use a proper many-to-many relationship, involving three tables:
THINGS
table)AUSTRALIAN_STATES
table)You'll need JOINs to get data out of the three tables, but if you want to know things like how many are associated to a particular state, or two particular states, it's the proper model.
Upvotes: 7
Reputation: 15780
By using REGEXP
$result = mysql_query("SELECT * FROM table WHERE column REGEXP $your_search_string");
Upvotes: -2