这两天项目开发中,需要实现一些比较实用的功能,用了两个使用的sql,总结一下,怕下次忘记了。
1. 检索数据库中跟提交的内容相匹配的内容
比如:提交的数据是“游泳”,那么数据库中有“我喜欢游泳”字样的就算是匹配,但是这样一来,还是不够,比如我提交的是“周末去游泳”,数据库中有“游泳”的内容,其实意思类似,但是却使用like找不到的,于是想到下面的sql,已经封装成函数了:
function getRelationTags($tagTitle,$cols="*")
{
$titleFeildStrLen = 24; //3*8 四个汉字或者24个字符.
if ("" == $tagTitle) return false;
$sql = "select $cols from ".$TableName." where title != '' and (LOCATE(title,'$tagTitle') or ((issystem = 1 or LENGTH(title) $data =& $db->getAll($sql);
if(DB::isError($data)){
return $this->returnValue($data->getMessage());
}else{
return $data;
}
}
看sql:
select $cols from ".$TableName." where title != '' and (LOCATE(title,'$tagTitle') or ((issystem = 1 or LENGTH(title)
其实就是两次匹配,一次是正向匹配,就是把提交的标签跟数据库中标签进行匹配,第二次是把数据库中的标签跟提交的标签进行匹配。
关键在LOCATE()函数,同时也限制了长度,因为mysql中的编码是:
set names 'utf8'
就是是utf8的,那么一个汉字要占用3个字节,字符只占用1个字节,所以上面的:
$titleFeildStrLen = 24;
就是8个汉字和24个字符范围那的标签进行匹配。
2. 同类排序
数据库中比如是这样的内容:
北京 1023 1
天津 2301 1
上海 3450 1
天津 4520 1
北京 3902 1
那么我要提取所有的城市数据,并且把每种城市数据的总数跟别的城市总数进行比较后排序。
函数代码如下:
function getMostCity($num)
{
$sql = "select count(id) as num,city from ".$TableName." where city != '' group by city order by num desc limit 0,$num;";
$data =& $db->getAll($sql);
if($db->isError($data))
return false;
else
return $data;
}
我们关注一下上面的sql语句:
select count(id) as num,city from ".$TableName." where city != '' group by city order by num desc limit 0,$num
核心就是 group by city 把类似城市集中起来后按照多到少排序。
Author: heiyeluren
writeTime: 2005-07-01 14:35

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor