我的样本表包含这样的数据
id uniqueid values 1 6 0 2 6 1 3 6 2 4 6 0 5 6 1
我想要这样的结果
id uniqueid values 2 6 1 3 6 2 4 6 0
我试过这个
select id,uniqueid,values
FROM t1
WHERE
id not in(SELECT concat(MAX(message_id_pk),',',min(message_id_pk)) FROM t1
where uniqueid=6)
and `uniqueid`=6
GROUP BY uniqueid
但它不起作用
答案 0 :(得分:1)
您可以通过自联接获得所需的结果,内部查询将获得每个组的最大值和最小值,外部查询将使用minid
和maxid
来过滤掉结果p>
select a.*
from demo a
join (
select `uniqueid`,min(id) minid, max(id) maxid
from demo
where uniqueid=6
group by `uniqueid`
) b using(`uniqueid`)
where a.id > b.minid and a.id < b.maxid /* a.id <> b.minid and a.id <> b.maxid */
答案 1 :(得分:0)
您也可以使用 EXISTS
的2个子查询来排除每个uniqueid
的最小和最大ID。
<强>查询强>
select `id`, `uniqueid`, `values`
from `your_table_name` t1
where exists (
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` > t1.`id`
)
and exists(
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` < t1.`id`
);
Here
是一个sql小提琴演示
答案 2 :(得分:-1)
试试这个 -
SELECT id, uniqueid, values
FROM YOUR_TABLE
WHERE id NOT IN (MIN(id), MAX(id));