需要有关MySQL搜索查询的帮助

时间:2010-02-26 12:56:48

标签: mysql

我正在尝试设置的MySQL查询需要一些帮助。我需要查找匹配条件的记录,这些条件位于具有多对多关系的两个不同表中。

我在这个查询中使用了三个表,第一个包含项目,第二个包含主题,第三个将它们连接在一起。我希望查询能够找到与用户选择的主题相关的项目。用户还可以选择多个主题来执行搜索。在这种情况下,我只想显示与这两个主题相关的项目,而不是其中任何一个。这是我无法弄清楚该怎么做的。我预计我必须做这样的事情,但是这个查询没有结果,而有一个项目链接到数据库中的两个主题。

SELECT `projects`.*
FROM `projects`, `topics`, `projects_topics`
WHERE (`name` LIKE '%%' || `vision` LIKE '%%' || `highlights` LIKE '%%' || `optional` LIKE '%%')
    && ((`projects_topics`.`projects_id` = `projects`.`id` && `projects_topics`.`topics_id` = `topics`.`id` && `topics`.`id` = '1')
        && (`projects_topics`.`projects_id` = `projects`.`id` && `projects_topics`.`topics_id` = `topics`.`id` && `topics`.`id` = '9'))
ORDER BY `date_added` DESC

这些是表格:

  

项目

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment | 
| fields_id    | int(11)      | NO   | PRI | NULL    |                | 
| name         | varchar(255) | YES  |     | NULL    |                | 
| address      | varchar(255) | YES  |     | NULL    |                | 
| zip          | varchar(255) | YES  |     | NULL    |                | 
| city         | varchar(255) | YES  |     | NULL    |                | 
| state        | varchar(255) | YES  |     | NULL    |                | 
| countries_id | int(11)      | NO   | PRI | NULL    |                | 
| website      | varchar(255) | YES  |     | NULL    |                | 
| client       | varchar(255) | YES  |     | NULL    |                | 
| finished     | date         | YES  |     | NULL    |                | 
| budget       | int(11)      | YES  |     | NULL    |                | 
| vision       | text         | YES  |     | NULL    |                | 
| highlights   | text         | YES  |     | NULL    |                | 
| innovation   | text         | YES  |     | NULL    |                | 
| optional     | text         | YES  |     | NULL    |                | 
| publish      | tinyint(1)   | YES  |     | NULL    |                | 
| date         | datetime     | YES  |     | NULL    |                | 
| featured     | tinyint(1)   | YES  |     | NULL    |                | 
| frontpage    | tinyint(1)   | YES  |     | NULL    |                | 
| date_added   | datetime     | YES  |     | NULL    |                | 
+--------------+--------------+------+-----+---------+----------------+
  

主题

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment | 
| topic | varchar(255) | YES  |     | NULL    |                | 
| order | int(11)      | YES  |     | NULL    |                | 
+-------+--------------+------+-----+---------+----------------+
  

projects_topics

+-------------+---------+------+-----+---------+-------+
| Field       | Type    | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| projects_id | int(11) | NO   | PRI | NULL    |       | 
| topics_id   | int(11) | NO   | PRI | NULL    |       | 
+-------------+---------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:1)

SELECT  p.*
FROM    (
        SELECT  project_id
        FROM    project_topics pt
        WHERE   topics_id IN (5, 9)
        GROUP BY
                project_id
        HAVING  COUNT(*) = 2
        ) pto
JOIN    projects p
ON      p.project_id = pto.project_id

SELECT  p.*
FROM    projects p
WHERE   EXISTS
        (
        SELECT  NULL
        FROM    project_topics pt
        WHERE   pt.project_id = p.project_id
                AND pt.topic_id IN (5, 9)
        LIMIT 1 OFFSET 1
        )

确保PK中的project_topics定义为(project_id, topic_id)(按此顺序),或在UNIQUE上创建其他(topic_id, project_id)索引。< / p>