我有一个带有一些重复记录的mysql表,有一列EOF
。如果所有数据都正确,那么将为所有列插入ok。如果某些数据错误,则会为这些错误的数据记录插入错误。如果没有错误,则相关列中将显示ok。
但如果有错误,那么我想通过使用group by显示错误。我该如何为此编写查询?
样本表
Error_code
我使用group by
使用以下查询voucherid ean error_code
123456 234 OK
123456 435 OK
123456 345 Ok
此查询将返回此数据:
select voucherid, error_code
from voucher_info
group by voucher id
但如果我在数据中有错误代码,那么表格将如下所示
voucherid error_code
123456 ok
现在我想将error_code值显示为如下所示的错误。
voucherid ean error_code
123456 234 OK
123456 435 Error
123456 345 Ok
如何通过使用group by来做到这一点?任何帮助将不胜感激。
答案 0 :(得分:0)
我不确定你是否有2张桌子或只有一张桌子。如果你有一张桌子和 只想知道是否有错误,你可以这样做:
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static final String BASE_URL = "http://...";
private ArrayList<Userlist> userList;
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
recyclerView = (RecyclerView) findViewById(R.id.rv_user);
recyclerView.setHasFixedSize(true);
//LayoutManager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
//Adapter
adapter = new RecyclerViewAdapter(userList);
recyclerView.setLayoutManager(layoutManager);
ButterKnife.bind(this);
getAllUsers();
}
@Override
protected void onPostResume() {
super.onPostResume();
recyclerView.setAdapter(adapter);
}
private void getAllUsers() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(BASE_URL + "user")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseStr = response.body().string();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
List<Userlist> userList = new ArrayList<>();
userList = Arrays.asList(gson.fromJson(responseStr, Userlist.class));
System.out.print(responseStr);
response.body().close();
adapter.notifyDataSetChanged();
}
});
}}
<强>样品强>
SELECT
vi.voucherid
, GROUP_CONCAT(ean) eans
, IF( COUNT(*) - SUM(IF(error_code = 'ERROR' OR @err = 'ERROR' , 0 , 1)) = 0 , 'OK', 'ERROR') error_code
FROM voucher_info vi
CROSS JOIN ( SELECT @err:='') AS initial
GROUP BY voucherid;