以下查询用于提取以下列&行。
问题是我无法从每个用户ID中提取两行。 如果我使用限制,则只获取前两行。
所以我如何为每个唯一用户ID提取两行。
例如:
two rows of userid 222
two rows of userid 122
two rows of userid 367
等等
TEXT OP:
预期产出:
答案 0 :(得分: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
答案 1 :(得分: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
在答案不起作用的情况下,here对这种查询问题是一个很好的解读,也有各种解决方法。