0

我正在 php mysql 中为图片库网站创建一个网站,其中将有数千张图片。我的表结构在此链接的图片

现在我想要的是当用户搜索“粉红色”这个词时,它应该显示图像表中的两个图像。但是,如果用户搜索“粉红色的花”,它应该只显示第一张图片。这只是一个例子。用户甚至可以搜索“美丽的粉红色花朵”......我只需要 mysql 查询即可。我希望我的问题足够清楚。提前感谢您提供任何帮助和建议。

4

1 回答 1

0

你可以做这样的事情,在运行explode( ' ', $serachTags )或获取每个标签之后。

SELECT
   images.img_name,
   images.img_url
FROM
   images,
   connections,
   tags AS tagA,
   tags AS tagB,
   tags AS tagC,
   ...
WHERE
   images.img_id = connections.img_id AND
   tagA.tag_id = connections.tag_id AND
   tagA.tag_name = 'name1' AND
   tagB.tag_id = connections.tag_id AND
   tagB.tag_name = 'name2' AND
   tagC.tag_id = connections.tag_id AND
   tagC.tag_name = 'name3' AND
   ...

IE。对于花和粉红色,你可以这样做:

SELECT
   images.img_name,
   images.img_url
FROM
   images,
   connections,
   tags AS tagA,
   tags AS tagB
WHERE
   images.img_id = connections.img_id AND
   tagA.tag_id = connections.tag_id AND
   tagA.tag_name = 'flower' AND
   tagB.tag_id = connections.tag_id AND
   tagB.tag_name = 'pink'
于 2012-12-16T14:27:04.353 回答