如何计算同一个表中不同列中的外键?

时间:2017-08-29 03:58:08

标签: mysql

我正在开发这个系统,我为团队提供了一张桌子,为超级明星提供了一张桌子。在teams表中,我有4列具有引用超级星表的外键。

Table Teams Picture

我的目标是知道谁是团队中最受欢迎的超级巨星。目前我使用4个代码并手动汇总结果。

我正在使用的代码:

SELECT superstars.name,COUNT(superstar01) FROM smackdown_teams JOIN superstars on smackdown_teams.superstar01 = superstars.id GROUP by superstar01 Order by COUNT(superstar01) desc;
SELECT superstars.name,COUNT(superstar02) FROM smackdown_teams JOIN superstars on smackdown_teams.superstar02 = superstars.id GROUP by superstar02 Order by COUNT(superstar02) desc;
SELECT superstars.name,COUNT(superstar03) FROM smackdown_teams JOIN superstars on smackdown_teams.superstar03 = superstars.id GROUP by superstar03 Order by COUNT(superstar03) desc;
SELECT superstars.name,COUNT(superstar04) FROM smackdown_teams JOIN superstars on smackdown_teams.superstar04 = superstars.id GROUP by superstar04 Order by COUNT(superstar04) desc;

有关如何知道谁是被挑选最多的ID的任何建议?

1 个答案:

答案 0 :(得分:0)

您可以合并结果并找到结果的最大值。

   public class BaseListAdapter extends RecyclerView.Adapter<BaseListAdapter.ViewHolder> {
  private List<PList> menuItems;
  private Context mContext;
  private ActivitySingleGroup activitySingleGroup;


  //Bottom Sheets Views Declaration
  private TextView txtSelectedProduct;
  private TextView txtPRemark;
  private TextView txtQty;
  private EditText edtUserQty;
  private Button btnBuy;
  private Button btnDiscard;
  private ViewPager mPager;
  private CircleIndicator indicator;

  public BaseListAdapter(List<PList> menuItems, Context mContext) {
    this.menuItems = menuItems;
    this.mContext = mContext;
    this.activitySingleGroup = (ActivitySingleGroup) mContext;
  }

  public static class ViewHolder extends RecyclerView.ViewHolder {

    TextView txtName;
    TextView txtPrice;
    ImageView imgDefault;
    LinearLayout parentLayout;
    CoordinatorLayout coordinatorLayout;

    public ViewHolder(View v) {
      super(v);

      txtName = (TextView) v.findViewById(R.id.txtName);
      txtPrice = (TextView) v.findViewById(R.id.txtPrice);
      imgDefault = (ImageView) v.findViewById(R.id.img_defaultImage);
      parentLayout = (LinearLayout) v.findViewById(R.id.parentLayout);
      coordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.coordinatorLayout);

    }
  }

  @Override
  public BaseListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    // Create a new View
    final View v = LayoutInflater.from(activitySingleGroup).inflate(R.layout.activity_normal_group_recycler, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {


    //Download and Load Default Image from server into imgDefault ImageView
    Picasso picasso;
    OkHttpClient client = null;
    String url = "https://bartarinapp-irdeveloper.rhcloud.com/api/images/download/";

    //Handel situations that default image variables will be null
    if (menuItems.get(position).getPDefaultImage() != null &&
      menuItems.get(position).getPDefaultImage().getDefault() != null) {

      if ((menuItems.get(position).getPDefaultImage().getDefault()) &&
        (menuItems.get(position).getPDefaultImage().getIId() != null)) {

        url += menuItems.get(position).getPDefaultImage().getIId();

        client = ServerClass.downloadImage(
          menuItems.get(position).getPDefaultImage().getIId(),
          holder.imgDefault,
          activitySingleGroup);

        picasso = new Picasso.Builder(mContext)
          .downloader(new OkHttp3Downloader(client))
          .listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
              //Here your log
              Log.i("I_ERROR", "Error is: " + exception.toString());
            }
          })
          .build();

      } else {
        url = null;
        picasso = new Picasso.Builder(mContext)
          .listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
              //Here your log
              Log.i("I_ERROR", "Error is: " + exception.toString());
            }
          })
          .build();

      }
    } else {
      url = null;
      picasso = new Picasso.Builder(mContext)
        .listener(new Picasso.Listener() {
          @Override
          public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
            //Here your log
            Log.i("I_ERROR", "Error is: " + exception.toString());
          }
        })
        .build();
    }


    picasso.cancelRequest(holder.imgDefault);
    if (url != null && url.length() > 0) {

      //put here picaso image load code
      picasso.load(url)
        .placeholder(R.drawable.loading_01)
        .error(R.drawable.loading_02)
        .into(holder.imgDefault);

    } else {
      holder.imgDefault.setImageDrawable(null);
    }

    holder.txtName.setText(menuItems.get(position).getPName());
    holder.txtPrice.setText(String.valueOf(menuItems.get(position).getPPrice()));
    holder.parentLayout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {

        prepareBottomSheet(view, position, holder.coordinatorLayout);
      }
    });
  }

  @Override
  public int getItemCount() {
    if (menuItems.size() > 0) {

      return menuItems.size();

    } else {
      return 0;
    }

  }