我有一个表标签(id,pid),其中id是主键。
值示例 (id,pid) (1,23) (2,23) (3,65) (4,71) (5,71)
我想为每个不同的pid选择一个随机ID。 使用此查询:
SELECT a.pid,
(SELECT b.id FROM tab AS b WHERE a.pid=b.pid ORDER BY rand() LIMIT 1) AS id
FROM tab AS r
GROUP BY r.pid
ORDER BY r.pid;
例如,我得到(2,23),(3,65),(4,71)
如何使用Django语法编写此查询?