【问题标题】:create as in 2 meta_key using mysql使用 mysql 在 2 meta_key 中创建
【发布时间】:2017-08-19 21:46:22
【问题描述】:

像这样从 wordpress 中假设表 mysql:

post_id | meta_key     | meta_value
19      | poster_url   | http://exampleimage.com
19      | vote_average | 7.5

我试过用

 SELECT * FROM (Select ID,post_date,post_date_gmt,post_content,post_title,post_status,post_name,post_type,meta_key,meta_value as picture from get_movies where meta_key='poster_url' and post_status='publish' and post_type='post') as a,
(Select meta_value as rate from get_movies where meta_key='vote_average' and post_status='publish' and post_type='post') as b

但结果已重复并错过了 'rate' .. 在这种情况下,我只需要互相制作别名.. 比如 poster_url = 图片,并且 vote_average = rate

有可能吗?

【问题讨论】:

  • 你想要什么结果?
  • @GordonLinoff 喜欢 post_id |率 |图片 19 | 7.5 | exmapleimage.com
  • @GordonLinoff 现在我遇到了一个小问题.. 我如何在“WHERE”之后使用这个条件.. 示例:where post_status = 'publish' and post_type = 'post' and meta_key in ('poster_url', 'vote_average','category') and post_title like '%7%' OR VOTE_AVERAGE LIKE '%7%'

标签: php mysql sql wordpress


【解决方案1】:

出于某种原因,我认为这可能是您想要的:

Select ID, post_date, post_date_gmt, post_content, post_title,
       post_status, post_name, post_type,
       max(case when meta_key = 'poster_url' then meta_value end) as picture,
       max(case when meta_key = 'vote_average' then meta_value end) as rating
from get_movies
where post_status = 'publish' and post_type = 'post' and
      meta_key in ('poster_url', 'vote_average') 
group by ID, post_date, post_date_gmt, post_content, post_title,
         post_status, post_name, post_type;

它返回两个不同列中的元键值。

【讨论】:

  • 现在我遇到了一个小问题 .. 我如何在 'WHERE' 之后使用这个条件 .. 示例:where post_status = 'publish' and post_type = 'post' and meta_key in ('poster_url', 'vote_average','category') and post_title like '%7%' OR VOTE_AVERAGE LIKE '%7%'
  • @Edofx 。 . .您将使用HAVING 子句。
  • @Edofx 。 . .这有点复杂。也许你应该问另一个问题。
猜你喜欢
  • 2013-06-26
  • 2013-04-08
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 2023-03-06
  • 2013-07-08
相关资源
最近更新 更多