Mysql获得顶级产品

时间:2017-06-16 19:11:53

标签: php mysql

让我从我的问题中告诉你一个例子 例如,我们有一个名为(order)的表,它将所有订单和购买插入此表。

表A(订单):

+--------------------------+
| CustomerKey | ProductKey |
+--------------------------+
| 306545      | pro1       |
| 597864      | pro3       |
| 784678      | pro2       |
| 905479      | pro3       |
| 306545      | pro1       |
| 348965      | pro3       |
| 784678      | pro3       |
+--------------------------+

现在我想订购并获得我们的畅销产品

查询输出:

+-------------------------------+
| id | ProductKey | numberSold  |
+-------------------------------+
| 1  | pro3       | 4           |
| 2  | pro1       | 2           |
| 3  | pro2       | 1           |
+-------------------------------+     

我该怎么做?

2 个答案:

答案 0 :(得分:2)

使用此:

select ProductKey, count(1) as numberSold from A group by ProductKey order by count(1) desc

查询按 ProductKey 对相同产品进行分组,并将已售出的总数 count(1)相加。 然后,显示按销售的最大销售量排序的结果。

答案 1 :(得分:-1)

您可以使用ORDER BY关键字。

示例:

#include<iostream>
#include<conio.h>

using namespace std;
class State {
public:
    virtual void pull(class Fan* f) = 0;
};

class Off;
class Medium;
class High;

class Fan {
private:
    State* current_state;
public:
    void set_state(State* s) {
        current_state = s;
    }
    Fan() {
        current_state = new Off();
    }
    void pull() {
        current_state->pull(this);
    }

};

class Off:public State{
public:
    void pull(Fan *f){
        f->set_state(new Medium());
        cout << "Medium speed" << endl;
    }
};
class Medium:public State {
public:
    void pull(Fan* f) {
        f->set_state(new High());
        cout << "Max speed" << endl;
    }
};
class High :public State {
public:
    void pull(Fan *f) {
        f->set_state(new Off());
        cout << "The fan is off" << endl;
    }
};

int main() {

    Fan* fan = new Fan();
    int c;
    while (1)
    {
        cin>>c;
        fan->pull();
    }

    return 0;
}

了解更多信息:link