我的表(Data_A)由列组成:
_id | mac_address | bit
有多个行具有相同的mac_address
,位可能是0/1
我想构建一个sqlite查询,我想在其中选择所有行的_id,使每行具有唯一的mac_address
,并且应该选择PREFERENCE来选择具有bit = 1
的行。
答案 0 :(得分:3)
我认为以下适用于SQLite:
select a.*
from data_a a
where a._id = (select aa._id
from data_a aa
where aa.mac_address = a.mac_address
order by aa.bit desc
limit 1
);
答案 1 :(得分:0)
在SQLite 3.7.11或更高版本中,您只需使用min()/ max()从组中选择整行:
SELECT _id, mac_address, max(bit)
FROM Data_A
GROUP BY mac_address