Php 使用列值作为参数-mysql统计所有字符串的出现次数
我正在为一个项目收集一些数据信息,我想看看哪些单词在数据库中使用列值作为搜索参数重复得更多,如:Php 使用列值作为参数-mysql统计所有字符串的出现次数,php,mysql,search,Php,Mysql,Search,我正在为一个项目收集一些数据信息,我想看看哪些单词在数据库中使用列值作为搜索参数重复得更多,如: table_product id | name | description 1 | blue shirt | cool shirt 2 | yellow pants | pretty nice pants 3 | red shirt | cool red shirt 预期结果如下: field | count shirt | 4
table_product
id | name | description
1 | blue shirt | cool shirt
2 | yellow pants | pretty nice pants
3 | red shirt | cool red shirt
预期结果如下:
field | count
shirt | 4
cool | 2
pants | 2
... | ...
有没有一种方法可以只使用query来实现这一点?
谢谢
编辑:我不能预先设置单词列表,目标是交叉所有字符串并计算出现的次数。要生成这种类型的结果,您必须将每个单词合并为一个单独的查询。就这样
SELECT
'shirt' AS field,
SUM(
CASE WHEN LOCATE('shirt', `name`) > 1 AND LOCATE('shirt', `description`) THEN 2
WHEN LOCATE('shirt', `name`) > 1 OR LOCATE('shirt', `description`) THEN 1
ELSE 0
END ) AS 'Count'
FROM table_product
UNION
SELECT
'cool' AS field,
SUM(
CASE WHEN LOCATE('cool', `name`) > 1 AND LOCATE('cool', `description`) THEN 2
WHEN LOCATE('cool', `name`) > 1 OR LOCATE('cool', `description`) THEN 1
ELSE 0
END ) AS 'Count'
FROM table_product
UNION
SELECT
'pants' AS field,
SUM(
CASE WHEN LOCATE('pants', `name`) > 1 AND LOCATE('pants', `description`) THEN 2
WHEN LOCATE('pants', `name`) > 1 OR LOCATE('pants', `description`) THEN 1
ELSE 0
END ) AS 'Count'
FROM table_product
建议:
我建议你只为你想要的每一个计数做一个新的列。。换言之,只需使用一个数据透视结果。。因为这将更容易处理,如果您试图在另一种编程语言中获得结果,关键将是名称aka‘shirt’、‘cool’。。。该值将是计数。。示范
SELECT
SUM(
CASE WHEN LOCATE('shirt', `name`) > 1 AND LOCATE('shirt', `description`) THEN 2
WHEN LOCATE('shirt', `name`) > 1 OR LOCATE('shirt', `description`) THEN 1
ELSE 0
END ) AS 'shirt',
SUM(
CASE WHEN LOCATE('cool', `name`) > 1 AND LOCATE('cool', `description`) THEN 2
WHEN LOCATE('cool', `name`) > 1 OR LOCATE('cool', `description`) THEN 1
ELSE 0
END ) AS 'cool',
SUM(
CASE WHEN LOCATE('pants', `name`) > 1 AND LOCATE('pants', `description`) THEN 2
WHEN LOCATE('pants', `name`) > 1 OR LOCATE('pants', `description`) THEN 1
ELSE 0
END ) AS 'pants'
FROM table_product;
谢谢约翰提供的信息和你的时间 对于可能有用的人,我开发了一个快速脚本来用php实现这个技巧 附言:这绝不是你能做到的最好的方法,但它会起作用的
//search the strings
$result = mysql_query("SELECT field_a, field_b FROM table");
$arrayMaster = array();
while ($row = mysql_fetch_array($result)) {
//explode the spaces to get each word
$field_a = explode(' ', $row['field_a']);
foreach ($field_a as $string) {
$string = strtolower($string);
//remove words with less then 4 charac
if(strlen($string) < 4){
continue;
}
//check if there is a position on the array with that string, if it exists ++
if( isset($arrayMaster[$string]) ){
$arrayMaster[$string]++;
} else {
$arrayMaster[$string] = 1;
}
}
$field_b = explode(' ', $row['field_b']);
foreach ($field_b as $string) {
$string = strtolower($string);
if(strlen($string) < 4){
continue;
}
if( isset($arrayMaster[$string]) ){
$arrayMaster[$string]++;
} else {
$arrayMaster[$string] = 1;
}
}
}
//sort the array in reverse order
arsort($arrayMaster);
//print the words and the amount that they show up
print_r($arrayMaster);
//搜索字符串
$result=mysql_查询(“从表中选择字段a、字段b”);
$arrayMaster=array();
while($row=mysql\u fetch\u数组($result)){
//分解空格以获得每个单词
$field_a=爆炸(“”,$row['field_a']);
foreach($field\u作为$string){
$string=strtolower($string);
//删除少于4个字符的单词
if(strlen($string)<4){
继续;
}
//检查数组中是否存在包含该字符串的位置(如果存在)++
if(isset($arrayMaster[$string]){
$arrayMaster[$string]++;
}否则{
$arrayMaster[$string]=1;
}
}
$field_b=爆炸(“”,$row['field_b']);
foreach($field_b作为$string){
$string=strtolower($string);
if(strlen($string)<4){
继续;
}
if(isset($arrayMaster[$string]){
$arrayMaster[$string]++;
}否则{
$arrayMaster[$string]=1;
}
}
}
//按相反顺序对数组排序
arsort($arrayMaster);
//打印单词及其显示的数量
打印(arrayMaster);
你好,约翰,谢谢你的回复。如果已经设置了单词,则此代码可以工作,但我想将所有字符串相互交叉,并计算它们在两列中出现的次数。@amartini不,这在MySQL中是不可能的。。你必须对你想要比较的东西有所投入。。查找单词和子字符串函数有不同的方法,但都会使用所需的字符串进行查找、比较或提取。