如何在MySQL中获取行出现值?

时间:2017-04-12 09:11:34

标签: mysql sql

我有一张下表

public class Activity extends AppCompatActivity { // maybe extend other activity
    private static final String RESULT_TYPE_PLACES = "places";
    private static final String RESULT_TYPE_PLACES_DETAILS = "places_details";
    private static final String RESULT_TYPE_UNKNOWN = "NAN";

    private String currentResult = RESULT_TYPE_UNKNOWN;

    //.....some code....

   @Override
   protected void onPostExecute(String result)
     currentResult = result;
     checkMapAndLoad();

    @Override
    public void onMapReady(GoogleMap googleMap) {
       mMap=googleMap;
       checkMapAndLoad():
    }

   private void checkMapAndLoad() {
     if(mMap != null && !RESULT_TYPE_UNKNOWN.equals(currentResult)) {
          new ParserTask(currentResult).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
     }
   }       
}

我需要的是另外一列,根据名称显示出现值,如下所示

id   name   amount
1    aaa     1000
2    bbb     1500
3    ccc     1700
4    ddd     2000
5    aaa     1400
6    aaa     1700
7    bbb     1800

1 个答案:

答案 0 :(得分:3)

您可以使用相关子查询来执行此操作:

SELECT id, name, amount, 
       (SELECT COUNT(*)
        FROM mytable AS t2
        WHERE t2.name = t1.name AND t2.id <= t1.id) AS occurrence
FROM mytable AS t1   

Demo here