Mysql查询图片搜索

时间:2012-12-16 14:01:47

标签: mysql

我正在为图片库网站制作一个php mysql网站,那里会有成千上万的图片。 我的表格结构在此链接的图像中 here

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

1 个答案:

答案 0 :(得分: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
   ...

予。即你可以做花和粉红色:

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'