What is the proper way of doing the following in a mysql query?:
$string="apple banana orange";
mysql column A = "banana"
mysql query: column A not included in $string
Thanks!
What is the proper way of doing the following in a mysql query?:
$string="apple banana orange";
mysql column A = "banana"
mysql query: column A not included in $string
Thanks!
If each row has a column called A that has space separated tokens you can run this:
SELECT * FROM table_name
WHERE LOCATE(CONCAT(' ',A,' '),'apple banana orange') = 0;
The reason I padded a blank on both sides would be to cover a situation like this:
SELECT * FROM table_name
WHERE LOCATE(CONCAT(' ',A,' '),'pineapple banana orange') = 0;
As a character string
apple is in apple banana orangeapple is in pineapple banana orangeIn terms of semantics
apple is in apple banana orangeapple is not in pineapple banana orangeWhat if you were looking for grape ?
As a character string
grape is in apple banana orange grapegrape is in apple banana orange grapefruitIn terms of semantics
grape is in apple banana orange grapegrape is not in apple banana orange grapefruitGive it a Try !!!
The query itself would yield a full table scan.
You should look into doing FULLTEXT indexes.
Maybe something like this? (I have not tested it though but I think it would work)
<?php
$string = "apple banana orange";
//Make an array of each fruit in string (with delimiter space)
$items = explode(" " , $string);
//None of the fruits in $string would be listed in resultset
$query = "SELECT * FROM fruits WHERE columnA NOT IN ($items)";
//db-handling...
?>
UPDATE: Don't make an array... ..do this instead...! $items = str_replace(" ", ",", $string);
IN () clause.