search
HomeBackend DevelopmentPHP Tutorial「相关文章」结果是越相关排名越前的案例,求一个高效的写法

我的文章系统,想按照多个件条下
,看看有什么可能性可做到

4张表  基础简介
content 文章主表
id
lid = 相关的文集id ,保存格式 (1,2,3)
tags = 相关的TAG 的ID   ,保存格式 (1,2,3)
typeid = 分类ID 只为1个数字 
文集
lid
name

Tags
id
name
分类
typeid
name

typeid = 分类ID, 不别多说
tag = 标签这不用多说,
lid = 一个文集, 但一篇文章可以放到多个文集, 比如: PHP+MYSQL+AJAX无刷新评论
用户可能放到  PHP文集,MYSQL文章,AJAX文集

TAG 是全站共用,重复就不新增,只会选择  
所以不能做成一个自己文章的分类列表,所以会出现了文集功能


因为文章某个部分已经会显示当前文集其他的文章

所以在关相文章的部分,文集中的文件不比TAG文章更应该排在前面

所以准备的关系排名是
1. TAG相关
2. 文集
3. 最新的当前分类文章

为什么要分3次???

因为我就是怕太TAG,同时又没有用到文集功能....
所以最后一步才是当前分类文章



当前文章的
TAG 是3,14
lid = 7,12
typeid = 2

SELECT * FROM `content` WHERE `tags` IN (3,14) ORDER BY `time` DESC LIMIT 10
SELECT * FROM `content` WHERE `lid` IN (7,12) ORDER BY `time` DESC LIMIT 10
SELECT * FROM `content` WHERE `typeid` = '2' ORDER BY `time` DESC LIMIT 10


前台显示十条相关文章
本来是准备,分3次,然后每次拿10条是最安全的

后来想想...这样还有合并数组什么的也麻烦,重点是取太多资料,但前台只需要10条

所以换成一句
SELECT * FROM `video_content` WHERE `tags` IN (3,14) or `lid` IN (3) or `typeid` = 2  LIMIT 10

但这样好像又能以TAG>文集>分类...去排名

请求一下有何方法?

要求好像蛮麻烦的呵呵,因为页面下半部有50%的部分是显示相关文章,所以在营运上很重要


回复讨论(解决方案)

暂时用了这一段代码,感觉有点蠢

有什么更好的方法建议呢?

//相关文章if (!empty($data["tags"]) || !empty($data["lid"])) {	$tags = $data["tags"];	$lid = $data["lid"];	$tag_sql = "SELECT * FROM `content` WHERE `tags` IN ($tags) OR `lid` IN ($lid) ORDER BY `time` DESC LIMIT 8";	$stmt = $pdo->prepare($tag_sql);	$stmt->execute();	$tags_v = $stmt->fetchAll(PDO::FETCH_ASSOC);	$smarty -> assign("r_v",$tags_v);}$tags_num = count($tags_v);if ($tags_num < 8 ) {	$now_v = "";	foreach ($tags_v as $key => $value) {  		$now_v .= $value['vid'].",";	}		$now_v=substr("$now_v",0,-1);	$need = 8-$tags_num;	$typeid = $data["typeid"];	$typeid_sql = "SELECT * FROM `content` WHERE `typeid` = '$typeid' AND  `vid` NOT IN ($now_v) ORDER BY `time` DESC LIMIT $need";	$stmt = $pdo->prepare($typeid_sql);	$stmt->execute();	$typeid_v = $stmt->fetchAll(PDO::FETCH_ASSOC);	$smarty -> assign("r_tv",$typeid_v);}

sql用or、not in本来就不好;
如果数据不需要太及时的话
把你3条sql语句组合好的数据缓存一段时间(半小时、一小时、甚至再长一点也可以)
因为你的数据都是死数据,没有浏览量,评论量等等

sql用or、not in本来就不好;
如果数据不需要太及时的话
把你3条sql语句组合好的数据缓存一段时间(半小时、一小时、甚至再长一点也可以)
因为你的数据都是死数据,没有浏览量,评论量等等

谢谢
这个我也了解

但更想知道的是,如果按照更有关的排得更前

就是先排TAG 再排LID  最后再排同类其他文章

莫非只能用二楼的方法?

你的 意思是不是如果要10条tag,就先取10条tag,不够再取有lib,接着不够再取有type的?

如果是这样的话, 你的sql是不是会有问题!

你的 意思是不是如果要10条tag,就先取10条tag,不够再取有lib,接着不够再取有type的?

如果是这样的话, 你的sql是不是会有问题!


是的

2楼暂时做了个简单的
暂时先不分tag和lid

不够再取type
以及2楼用8条做测试

是否有更好的做法?

用全文检索来做

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor