如何在具有一对多关系的表上执行内连接?

时间:2014-04-07 20:25:12

标签: php mysql database search

此搜索通过匹配元数据的值来使用数据库中的各种网页。这里的值字段有多个值,我不知道如何只获得GRADUATE科学课程或类似的东西。 我尝试了很多东西,但没有任何作用。

select distinct page.path,page.site_id,
  (select metadata_custom.value AS display_name
   from metadata_custom
   where field='academic search title'
     and page.id=metadata_custom.page_id) AS value,
   page.id
   from publish.page
   inner join metadata
     on page.metadata_id=metadata.id
   inner join metadata_custom
     on page.id=metadata_custom.page_id
   where field='acadsearch'
     and value like ('science%' AND 'graduate%");

另外,我还有另一个查询 价值如'{$ term}%'

一些示例表 表格页面:

id | account_id | site_id |  cms_id  | folder_id |  metadata_id | name |path | content
----------------------------------------------------------------------------------------------------
| 583 | 1 |   1 | e026b376c0a80123019b60bb23817853 | db2b800fc0a80123019b60bbb5e082cd| 2915 | rob-castillo| academics/undergraduate/social-work/rob-castillo | <region-render><div class="center_text">
<content/>
</div></region-render>   
----------------------------------------------------------------------------------------------------
| 578 |  1 |   1 | e026b25fc0a80123019b60bb58c9f847 | db2b800fc0a80123019b60bbb5e082cd | 2887 | jeffrey-bulanda | academics/undergraduate/social-work/jeffrey-bulanda | <region-render><div class="center_text">
<content/>
</div></region-render> 

Table Metadata Custom

id | account_id | site_id | file_id  | folder_id | page | field |value 
----------------------------------------------------------------------------------------------------

 21713 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | graduate                                                         |
| 21714 |          1 |       1 |    NULL |      NULL |     563 | acadsearch| rad2                                                                  |
| 21715 |          1 |       1 |    NULL |      NULL |     563 | override                 |                                                                       |
| 21716 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | humanities                                                            |
| 21717 |          1 |       1 |    NULL |      NULL |     563 | acadsearch                 |                                                                       |
| 21718 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | rad1                                                                 |
| 21719 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | gwcugcampus                                                           |
| 21720 |          1 |       1 |    NULL |      NULL |     563 | breadcrumb               |                                                                       |
| 21721 |          1 |       1 |    NULL |      NULL |     563 | acadsearch                 |                                                                       |
| 21722 |          1 |       1 |    NULL |      NULL |     563 | acadsearch   | Psychology (BA)                                                       |
| 21723 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | Psychology                                                            |
| 21724 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | rad1                                                                  |
| 21725 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | sciences                                                              |
| 21726 |          1 |       1 |    NULL |      NULL |     563 | acadsearch | gwcugcampus   

2 个答案:

答案 0 :(得分:2)

不确定整个查询应该做什么,但无论如何查询的最后部分都是错误的:

value like ('science%' AND 'graduate%')

应该是:

(value LIKE 'science%' OR value LIKE 'graduate%')

编辑:

根据更新的问题和下面的评论,我认为不需要通配符匹配或奇怪的连接。请尝试以下查询:

SELECT 
   p.`id`, p.`site_id`, p.`path`
FROM 
   `page` AS p,
   `metadata_custom` AS mc
WHERE
    p.`id` = mc.`page_id`
    AND mc.`field` =  'acadsearch'
    AND (
        mc.`value` =  'graduate'
        OR mc.`value` =  'rad1'
    )
GROUP BY
    p.`id`

要在更多元数据值上扩展搜索,请添加更多OR表达式。

问题中的查询包含一个子查询,如果没有每个页面的单个结果行,则无法添加该子查询。该子查询应该作为单独的查询执行,该查询可以在页面ID上与上述查询返回的每个页面匹配:

SELECT
    `page_id`,
    `value` AS display_name
FROM
    `metadata_custom`
WHERE 
    `field` = 'academic search title'
    AND `page_id` IN (<insert page IDs comma separated>)

答案 1 :(得分:1)

如果我正确地理解你,你想获得价值领域具有科学性并从中毕业的数据吗?如果他们必须是科学的话就是这样的话,那么毕业然后就是

SELECT DISTINCT page.path,
                page.site_id,
                (SELECT metadata_custom.value AS display_name
                 FROM   metadata_custom
                 WHERE  field = 'academic search title'
                    AND page.id = metadata_custom.page_id) AS value,
                page.id
FROM   publish.page
       INNER JOIN metadata
               ON page.metadata_id = metadata.id
       INNER JOIN metadata_custom
               ON page.id = metadata_custom.page_id
WHERE  field = 'acadsearch'
   AND value LIKE 'science%graduate%'

或者如果它们无关紧要那么这个

SELECT DISTINCT page.path,
                page.site_id,
                (SELECT metadata_custom.value AS display_name
                 FROM   metadata_custom
                 WHERE  field = 'academic search title'
                    AND page.id = metadata_custom.page_id) AS value,
                page.id
FROM   publish.page
       INNER JOIN metadata
               ON page.metadata_id = metadata.id
       INNER JOIN metadata_custom
               ON page.id = metadata_custom.page_id
WHERE  field = 'acadsearch'
   AND (value LIKE '%science%'
       AND value LIKE '%graduate%')

这里有关于mysql的文档 Mysql like docs