【问题标题】:innerjoin or another way to combine data内部联接或其他组合数据的方式
【发布时间】:2016-11-28 19:20:10
【问题描述】:

我正在尝试将两个表合并到一个搜索查询中。

表 1:常见问题解答

| faq_id | title | status | url_key |
| ------ | ----- | ------ | ------- |
|   1    | Test  |   1    | go.html |

表 2:faq_value

| faq_value_id | faq_id | attribute  | value          |
| ------------ | ------ | ---------  | -------        |
|      1       |    1   |  metakey   | testing        |
|      2       |    1   |  metadesc  | this is a test |
|      3       |    1   | meta_image | test.jpg        |

现在我想要的查询结果如下所示:

| faq_id | title | status | metadesc  | metakey | meta_image | url_key |
| ------ | ----- | ------ | --------  | ------- | ---------- | ------- |
|   1    | Test  |   1    | this is a | testing | test.jpg   | go.html |

到目前为止我的尝试失败了:

SELECT 
faq.faq_id,faq.title,status,
(SELECT faq_value.value AS metadesc WHERE faq_value.attribute_code = 'metadesc'),
(SELECT faq_value.value AS metakey WHERE faq_value.attribute_code = 'metakey'),
(SELECT faq_value.value AS meta_image FROM faq_value WHERE faq_value.attribute_code = 'meta_image')
faq.url_key,
FROM faq
INNER JOIN faq_value ON faq_value.faq_id = faq.faq_id

我确定我只是忽略了一些愚蠢的事情,但就是无法发现。这个踢出一个SQL错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE faq_value.attribute_code = 'metadesc'), (SELECT faq_value.value AS metakey' at line 2

【问题讨论】:

    标签: mysql datatable inner-join


    【解决方案1】:

    您可以在 faq_value 表上使用 3 个内连接

       SELECT 
        faq.faq_id
      , faq.title
      , faq.status
      , b.value as metadesc
      , c.value as metakey
      , d.value as metakey
      , faq.url
    from faq
    INNER JOIN faq_value as b on  b.faq_id = faq.faq_id and b.attribute_code = 'metadesc'
    INNER JOIN faq_value as c on  c.faq_id = faq.faq_id and c.attribute_code = 'metakey'
    INNER JOIN faq_value as d on  d.faq_id = faq.faq_id and d.attribute_code = 'meta_image'    
    

    【讨论】:

      【解决方案2】:

      http://sqlfiddle.com/#!9/c615f/4

      SELECT 
          faq.faq_id
        , faq.title
        , faq.status
        , fv.metadesc
        , fv.metakey
        , fv.metakey
        , faq.url_key
      FROM faq
      INNER JOIN 
      (SELECT 
          faq_id,
          MAX(IF(attribute = 'metadesc',value,null)) metadesc,
          MAX(IF(attribute = 'metakey',value,null)) metakey,
          MAX(IF(attribute = 'meta_image', value, null)) meta_image 
       FROM faq_value
       GROUP BY faq_id) fv
      ON  fv.faq_id = faq.faq_id
      

      【讨论】:

        【解决方案3】:

        一个简单的方法是基于属性列进行旋转

        使用 UNION ALL 构建的虚拟表的示例(别名为 A ...这将是您的 faq_value 表):

        SELECT FAQ_ID,
        MAX( CASE WHEN Attribute = 'MetaKey' THEN Attribute ELSE '' END) As MetaKey ,
        MAX( CASE WHEN Attribute = 'MetaDesc' THEN Attribute ELSE '' END) As MetaDesc  ,
        MAX( CASE WHEN Attribute = 'meta_image' THEN Attribute ELSE '' END) As metaimage 
        
        FROM 
        ( 
        
        Select 'MetaKey' as Attribute  , 1 as FAQ_ID UNION ALL
        Select 'MetaDEsc' as Attribute , 1 as FAQ_ID UNION ALL
        Select 'meta_image' as Attribute , 1 as FAQ_ID 
        
        ) A 
        
        GROUP BY FAQ_ID
        

        现在您需要做的就是加入 FAQ 表以获取其他列

        【讨论】:

          猜你喜欢
          • 2012-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-24
          • 2013-07-08
          • 1970-01-01
          • 2019-06-09
          相关资源
          最近更新 更多