2

以下查询用于提取以下列和行。

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

那么我如何为每个唯一用户 ID 提取两行。

例如 :

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

等等

文字操作:

预期输出:

4

2 回答 2

0

假设id是您的用户 id 并且post_ID是唯一的。

(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

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

(
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
于 2013-04-25T07:06:38.340 回答
0

不确定这在 mysql 上是否可行,但你可以这样做。我假设id是用户 ID 并且post_ID是唯一的。

编辑

尝试解决mysql限制。

select id,name,post_Imagename_small,Image_title,post_ID from users u1
where u1.post_ID in (
    select u3.post_ID from (
        select u2.post_ID from users u2
        where u2.id = u1.id
        limit 2
    ) s u3
)
order by u1.id

编辑 2

在答案不起作用的情况下,是关于这类查询问题的一个很好的阅读,也有各种解决方法。

于 2013-04-25T07:11:56.977 回答