我有一个数据库:带有表格的数据库:表格和一些字段:
id fname dphone count_pic dup_id
6055903 Karla 5126xxx798 1 57
6173767 Aaliyah 4082xxx534 4 39
5611411 Aaliyah 4082xxx534 15 39
5611211 Aaliyah 4082xxx534 18 39
4234798 Abby 3057xxx974 31 16
6166691 Walter 6178xxx280 1 74
3375576 Walter 6178xxx280 17 74
我要做的是选择具有最小count_pic的字段和具有更大count_pic且具有相同dup_id和fname的字段
如何在mysql中做到这一点?
我得到的答案与那些东西相匹配,但不是fname:
SELECT *
FROM `table` t1
INNER JOIN (SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id
AND (t1.count_pic = minpic
OR t1.count_pic = maxpic)
如何在循环中添加fname
的任何想法?现在它只跟踪相同的dup_id
感谢。
答案 0 :(得分:-1)
SELECT *
FROM table
WHERE (fname, countpic, dupid) in (
SELECT fname, max(countpic), dupid FROM table GROUP BY fname, dupid
union
SELECT fname, min(countpic), dupid FROM table GROUP BY fname, dupid
)