[英]mysql, select two different rows with same id but different values in another column
Preemptive apology for the title there... wasn't sure what to put.... 那里的标题抢先道歉...不确定要放什么...。
I am not the best with mysql and I have a problem that I am not able to wrap my head around. 我不是mysql最好的人,但有一个问题,就是我无法解决问题。
I have one table....which is the metadata table for a wordpress site... In the table I am storing the FB like count and the permalink for each post.... so it looks something like this for one post... 我有一个表...。这是wordpress网站的元数据表...在该表中,我存储了每个帖子的FB(如count和永久链接)。 ..
+-------------+----------------+-----------------+----------------------------------+ | meta_id | post_id | meta_key | meta_value | +-------------+----------------+-----------------+----------------------------------+ | 1 | 446 | _fb_count | 2 | | 2 | 446 | _permalink | /2013/08/image-aligned-left/446/ | +-------------+----------------+-----------------+----------------------------------+
What I would like to do is query this table and return both these values so that the returned data would be (post_id,Facebook Like Count, Permalink)... So it would look like: 我想做的就是查询该表并返回这两个值,以便返回的数据为(post_id,Facebook Like Count,Permalink)...因此看起来像:
+----------------+-----------------+----------------------------------+ | post_id | _fb_count | _permalink | +----------------+-----------------+----------------------------------+ | 446 | 2 | /2013/08/image-aligned-left/446/ | +----------------+-----------------+----------------------------------+
I looked around and am guessing I need to do some kind of inner join on the same table.... but I am not getting the results I want... 我环顾四周,猜测我需要在同一张桌子上进行某种内部联接。...但是我没有得到想要的结果...
I tried something like this with no success: 我尝试这样的事情没有成功:
SELECT pl.post_id,pl.meta_value,FBL.meta_value from wp_postmeta pl inner join wp_postmeta FBL on pl.post_id = FBL.post_id WHERE pl.meta_key = '_permalink' OR FBL.meta_key = '_fb_count
Any help would be appreciated... 任何帮助,将不胜感激...
You have to use conditional aggregation for that 您必须为此使用条件聚合
SELECT post_id,
MAX(CASE WHEN meta_key = '_fb_count' THEN meta_value END) `_fb_count`,
MAX(CASE WHEN meta_key = '_permalink' THEN meta_value END) `_permalink`
FROM wp_postmeta
WHERE meta_key IN('_fb_count', '_permalink')
GROUP BY post_id
Output: 输出:
| POST_ID | _FB_COUNT | _PERMALINK | |---------|-----------|----------------------------------| | 446 | 2 | /2013/08/image-aligned-left/446/ |
Here is SQLFiddle demo 这是SQLFiddle演示
But I agree with @Rob you probably better off using WP's appropriate functions for that. 但是我同意@Rob,您最好使用WP的适当功能。
Use following query: 使用以下查询:
SELECT e.post_id, e.meta_value as _fb_count, m.meta_value as _permalink FROM table
e inner join 'table' m where e.meta_key = '_fb_count' and m.meta_key= '_permalink' and e.post_id= m.post_id 选择e.post_id,e.meta_value作为_fb_count,m.meta_value作为_permalink FROM table
e内部联接'表'm其中e.meta_key ='_fb_count'和m.meta_key ='_permalink'和e.post_id = m.post_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.