MySQL计数和分组由两个不同的列组成

时间:2015-10-07 13:01:36

标签: mysql database group-by

我的表格recommender包含字段attractionidattractionid

我想计算attractionid表格组中存在多少recommender,但如果有attractionidattractionid recommender 1 1 1 2 1 1 2 3 2 1 2 2 2 2 2 2 对,则将它们计为1 。 例如,

 attractionid   count
      1            2
      2            3

预期结果:

 attractionid    recommender
          1               1
          1               1
          2               2
          2               2
          2               2

下面的行应计为1

SpriteBatch batch;
Texture background;
Sprite backgroundsprite;
Sprite ballsprite;
Texture line;
Texture ballimg;
BitmapFont credits;
BitmapFont input;
BitmapFont play;
float dt;
String string = "";
float ballx;
float bally;


float speedx;
float speedy;

Rectangle screenrect;
Rectangle ballrect;
float screenLeft ;
float screenBottom ;
float screenTop ;
float screenRight ;


@Override
public void create() {
    batch = new SpriteBatch();
    speedx = 5f * dt;
    speedy = 5f * dt;
    createsprite();
    createbackground();
    createtext();

    ballx = ballsprite.getX();
    bally = ballsprite.getY();

}
@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    dt = Gdx.graphics.getDeltaTime();



    ballsprite.setPosition(ballx + speedx,ballsprite.getY());
    ballsprite.translateX(speedx);

    float left = ballrect.getX();
    float bottom = ballrect.getY();
    float top = bottom + ballrect.getHeight();
    float right = left + ballrect.getWidth();



    if(left < screenLeft) {
        string = "left";
        speedx = 5f*dt;
    }
    if(right > screenRight)
    {
        string = "right";
        speedx = -5f*dt;
    }

    if(bottom < screenBottom)
    {
        string = "bottom";

    }
    if(top > screenTop)
    {
        string = "top";

    }



    batch.begin();
    backgroundsprite.draw(batch);
    ballsprite.draw(batch);
    rendertext();

    batch.end();
}

public void createbackground() {

    background = new Texture("images/BackgroundGodwin.jpg");
    backgroundsprite = new Sprite(background);
    backgroundsprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    screenrect = new Rectangle(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    screenLeft = screenrect.getX();
    screenBottom = screenrect.getY();
    screenTop = screenBottom + screenrect.getHeight();
    screenRight = screenLeft + screenrect.getWidth();

}

public void createsprite() {

    ballimg = new Texture("images/SpriteGodwin.png");
    ballsprite = new Sprite(ballimg);
    ballsprite.setScale(0.65f);
    ballsprite.setPosition(Gdx.graphics.getWidth()/3,Gdx.graphics.getHeight()/2);
    ballrect = new Rectangle(ballsprite.getBoundingRectangle());
}

@Override
public void dispose() {
    batch.dispose();
    ballimg.dispose();
    background.dispose();
    credits.dispose();
    play.dispose();
    input.dispose();
    line.dispose();
}

public void createtext(){
    play = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
    play.setColor(com.badlogic.gdx.graphics.Color.GOLD);

    credits = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
    credits.setColor(com.badlogic.gdx.graphics.Color.GOLD);

    input = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
    input.setColor(com.badlogic.gdx.graphics.Color.OLIVE);
}

public void rendertext() {
    credits.draw(batch, "Maded", Gdx.graphics.getWidth() / 7 - 50, Gdx.graphics.getHeight() / 9);
    play.draw(batch, "Touch the Screen to play!!", Gdx.graphics.getWidth() / 2 - 175, Gdx.graphics.getHeight() - 80);
    input.draw(batch, string, Gdx.graphics.getWidth() / 2 - 160, Gdx.graphics.getHeight() - 120);
}

2 个答案:

答案 0 :(得分:2)

distinct attractionid,recommender函数中使用count

<强>查询

select attractionid,
count(distinct attractionid,recommender) as `count`
from recommendation
group by attractionid;

答案 1 :(得分:1)

尝试:

select attractionid, count(recommender) cnt
from (
    select distinct attractionid, recommender
    from recommendation
) x
group by attractionid