如何在mysql中只选择2行相同的水果类型?

时间:2014-05-23 06:25:36

标签: mysql

从下表中如何只选择2行相同的水果类型?

id     fruitname      fruittype              
1      mango          organic                                         
2      apple          nonorgonic                 
3      mango          organic              
4      banana         organic           
5      mango          organic               
6      apple          nonorganic       




the output

id     fruitname      fruittype
1      mango          organic
2      apple          nonorganic
3      mango          organic
4      apple          nonorganic

这里香蕉只出现一次所以不想要 芒果有机3次发生所以只要2次

2 个答案:

答案 0 :(得分:1)

嗯..试试这个

 SELECT @a := @a + 1 as id, fruitname, fruittype
FROM  (SELECT @a := 0)  r JOIN (
SELECT fb1.fruitname,fb1.fruittype
    FROM testdb.fruit fb1
     GROUP by fb1.fruitname,fb1.fruittype
   HAVING count(fb1.fruitname) >=2
  UNION ALL
    SELECT fb1.fruitname,fb1.fruittype
      FROM testdb.fruit fb1
     GROUP by fb1.fruitname,fb1.fruittype
    HAVING count(fb1.fruitname) >=2) b
     ORDER BY fruitname,fruittype;

看看这是否是你需要的。

由于

答案 1 :(得分:0)

(select * from fruit where fruittype ='organic' limit 2) UNION (select * from fruit where fruittype='nonorganic' limit 2);

可能会更好地加入帮助。