mysql join也可以获取未连接的值

时间:2016-01-25 14:17:18

标签: mysql join

以下查询基于我的付款表。它与日常订单表连接。我的日常表中有账单号码列表。通过此查询,我只获得已付款的帐单号。想从日常表中获取完整的账单号码列表,如果没有收到付款,则收到的列为空。并使用Bill-no Ascending命令它们

create or replace view view_pymts
As SELECT p.`order-id`, p.`order-item-code`,daily.Bill_no,daily.tally_sku,daily.`quantity-purchased` as quantity,daily.`item-price`+daily.`shipping-price`as inv_value,daily.rma_rcvd_amt as return_value, round((SUM(p.amount) + z.other),2) AS received

FROM payments p
INNER JOIN daily ON p.`order-item-code`= daily.`order-item-id`
JOIN (
SELECT `order-id`, 
SUM(CASE WHEN `order-item-code` IS NULL
         THEN amount 
         ELSE 0.0 END) / (COUNT(DISTINCT `order-item-code`)) AS other 
FROM payments
GROUP BY `order-id`
) z
ON p.`order-id` = z.`order-id`
GROUP BY p.`order-id`, p.`order-item-code`

1 个答案:

答案 0 :(得分:0)

public class PlatesAdapter extends RecyclerView.Adapter<PlatesAdapter.ViewHolder> { //Declaring a List<> of Plates private List<Plates> mPlatesList; //Variable to hold total numberOfPlates being used int amountOfPlates = 0; String amountOfPlatesString; //OnBindViewHolder @Override public void onBindViewHolder(PlatesAdapter.ViewHolder holder, int position) { final TextView amountOfPlatesTextView = holder.amountOfPlatesTextView; //BUTTONS add 1 or subtract 1 from amountOfPlates; Button addAmountOfPlatesButton = holder.addButton; Button subtractAmountOfPlatesButton = holder.subButton; addAmountOfPlatesButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addPlates(); amountOfPlatesString = Integer.toString(amountOfPlates); amountOfPlatesTextView.setText(amountOfPlatesString); } }); subtractAmountOfPlatesButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { subtractPlates(); amountOfPlatesString = Integer.toString(amountOfPlates); amountOfPlatesTextView.setText(amountOfPlatesString); } }); } public void addPlatesLayout(Plates plate) { if (mPlatesList == null) mPlatesList = new ArrayList(); //plate.setPlateWeight(a); mPlatesList.add(plate); //notifyDataSetChanged(); notifyItemInserted(mPlatesList.size() - 1); } public int addPlates() { amountOfPlates++; return amountOfPlates; } public int subtractPlates() { amountOfPlates--; return amountOfPlates; } 替换为INNER JOIN。这将使连接右侧表中的所有缺失字段LEFT JOIN(连接语句之后的表,而不是之前的表,据说位于连接的左侧)。

编辑:我没有仔细阅读这个问题,而@minatverma是正确的。您可以使用NULL代替RIGHT JOIN(右连接执行与左连接相反的操作,将左侧表中缺少的字段设为空),或者切换表的顺序并使用INNER JOIN