简体   繁体   English

MySQL将两个记录合并为一个

[英]MySQL merge two records into one

I have status table with 我有状态表

id (primary key )
case id 
read status
delivered status

now I can have multiple records for same case id with different values 现在,我可以为具有相同值的相同案例ID设置多个记录

Here I have case ID (111) with two rows 这是两行的案例ID(111)

ID  case_id     read_status     delivered_status
1   111         1                0
2   111         0                1

How can I run query so it gives one row for above case with read status and delivery status to 1 ? 如何运行查询,以便在上述情况下将读状态和传递状态为1的行提供给1?

SqlFiddle SqlFiddle

http://sqlfiddle.com/#!9/9d977e/1 http://sqlfiddle.com/#!9/9d977e/1

read status and delivery status to 1 读取状态和传送状态为1

Do you mean the max value ? 您是指最大值吗? If so, here is the fiddle. 如果是这样,这是小提琴。

Fiddle 小提琴

The query : 查询:

select 
  `case_id`,
  max(`read_status`),
  max(`delivered_status`)
from status
group by `case_id`

您可以按case_id分组,并简单地获得分组中列的最大值

select id, case_id ,max(read_status) as read_status , max(delivered_status) as delivered_status  from status group by case_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM