简体   繁体   English

Mysql查询同一个id的多个值

[英]Mysql query for multiple values for the same id

I hope someone can help me with a mysql statement. 我希望有人可以用mysql语句来帮助我。 In the end I need it this way for example: (will be json encoded later). 最后我需要这样的方式,例如:(稍后将对json进行编码)。

{"meta_value": "Breitensteinstr 82031"} //street and plz

{"meta_value": "Danziger Str 65307"} //street and plz

{"meta_value": "71032"} //no street on this one

I can get the single values by doing 我可以通过这样做获得单个值

SELECT meta_value FROM `stroma_commentmeta` WHERE meta_key = 'plz' or meta_key = 'street';

but now I'm stuck. 但现在我被卡住了。

How can I get both values for the same comment_id in the same table? 如何在同一个表中获取同一个comment_id的两个值? My brain can't get the connections. 我的大脑无法获得联系。

Screen of the DB: DB的屏幕:

在此输入图像描述

 SELECT  group_concat( 
      meta_value 
         ORDER BY  
         meta_key DESC  
         SEPARATOR " "  ) as meta_value 
  FROM `stroma_commentmeta` 
  WHERE meta_key = 'plz' or meta_key = 'street' 
  GROUP BY comment_id ;

this comes with some limitations : 这带来了一些限制:

http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

alternatively just join them together ... 或者只是加入他们......

SELECT  straight_join concat( s.meta_value, " " , p.meta_value  ) as    meta_value  
FROM `stroma_commentmeta` p
JOIN `stroma_commentmeta` s on s.comment_id=p.comment_id and s.meta_key = 'street'
  WHERE p.meta_key = 'plz' ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM