$tag = 'sky';
选择 tags
包含 $tag
的行:
$sql = "select * from images where tags like '%" . $tag . "%' order by date desc";
如果我有一个标签数组怎么办:
$tags = array('sky', 'earth', 'sun'); // max 3
foreach($tags as $tag) {
$sql = "select * from images where tags like '%" . $tag . "%' order by date desc";
}
这是正确的方式吗,尤其是在性能方面。
表 images
大约有 20,000 行。
最佳答案
您可以使用正则表达式
。
$sql = "select * from images where tags REGEXP '" . implode('|', $tags) . "' order by date desc";
您的最终结果将是:
select * from images where tags REGEXP 'sky|earth|sun' order by date desc
关于php - 选择像数组一样的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50296971/