如何为每个唯一的用户ID提取两行 [英] How to extract two rows for each unique user id

查看:105
本文介绍了如何为每个唯一的用户ID提取两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下查询用于提取以下列&行.

Following query used to extract the following column & rows.

问题是我无法从每个用户ID提取两行. 如果我使用限制,则仅获取第一两行.

issue is i am not able extract two rows from each user ID . If i am using limit then fetches only 1st two rows.

所以我如何为每个唯一用户ID提取两行.

so how i can extract the two rows for each Unique user id.

例如:

two rows of userid 222
two rows of userid 122
two rows of userid 367

以此类推

TEXT OP:

预期输出:

推荐答案

假定 id 是您的用户ID,而 post_ID 是唯一的.

Assuming id is your user id and post_ID is unique.

(SELECT id,name,post,Image,post
    FROM users  
    GROUP BY id
    ORDER BY id
)
UNION
(SELECT id,name,post,Image,post
    FROM users
    WHERE post_ID NOT IN
    (SELECT post_ID
    FROM users  
    GROUP BY id
    ORDER BY id)
    GROUP BY id
    ORDER BY id
)
ORDER BY id, post_ID


请检查我的查询,从中得到以下答案:


Please check my query from which I get the below answer:

(
SELECT store_id, `key_id` , `string` , `translate` , `locale`
FROM `core_translate` AS C
GROUP BY `store_id`
ORDER BY `store_id`
)
UNION (

SELECT store_id, `key_id` , `string` , `translate` , `locale`
FROM `core_translate` AS C
WHERE `key_id` NOT
IN (

SELECT `key_id`
FROM `core_translate` AS C
GROUP BY `store_id`
ORDER BY `store_id`
)
GROUP BY `store_id`
ORDER BY C.`store_id`
)
ORDER BY store_id, `key_id`


store_id    key_id  string  translate   locale
0   20  Screensize  Screensize  en_US
0   21  Gender  Gender  en_US
1   1   Size    Size    en_US
1   2   Color   Color   en_US
2   5   Large Image     Large Image     en_US
2   6   Medium Image    Medium Image    en_US
3   10  Manufacturer    Manufacturer    en_US
3   11  Dimensions  Dimensions  en_US
4   15  Description     Description     en_US
4   16  Weight  Weight  en_US

这篇关于如何为每个唯一的用户ID提取两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆