繁体   English   中英

比较两行中的列,如果另一列的更改值不同,则进行比较

[英]Compare column from two rows and if different change values of another column

我有一个带有自动增量键id的表, item_no可以是一行中的一行或两行(因此它们总是具有连续的id ),它们共享相同的ref但具有不同的right/left (但从技术item_no可以重复多次)遍历整个表的时间,但这不是问题),并且在连续的行上有时description相同,但有时会有所不同:

id | item_no | description | right\left | ref
1  | 1       | a1          | right      | aaa
2  | 1       | a1          | left       | aaa
3  | 2       | b1          | right      | bbb
4  | 3       | c1          | right      | ccc
5  | 3       | c2          | left       | ccc
6  | 4       | d1          | right      | ddd
7  | 4       | d1          | left       | ddd

我的问题是,如果“匹配”行的description不同,则需要item_no在其值后附加-r或-l。

所以我想要的结果是:

id | item_no | description | right\left | ref
1  | 1       | a1          | right      | aaa
2  | 1       | a1          | left       | aaa
3  | 2       | b1          | right      | bbb
4  | 3-r     | c1          | right      | ccc
5  | 3-l     | c2          | left       | ccc
6  | 4       | d1          | right      | ddd
7  | 4       | d1          | left       | ddd

我将表导出到csv,但没有使用太多的php,而只是使用mysql语句,然后循环出结果,这是否可能在mysql语句中出现?还是我必须依靠php循环?

我会用这个:

update
  items inner join
  (select item_no from items
   group by item_no
   having count(distinct description)>1) dup
  on items.item_no=dup.item_no
set
  items.item_no=concat(items.item_no, '-', substr(rightleft, 1,1))

如果行始终是连续的,则也可以使用以下命令:

update
  items i1 inner join items i2
  on (i1.id=i2.id+1 or i1.id=i2.id-1) 
     and (i1.item_no=i2.item_no)
     and (i1.description<>i2.description)
set i1.item_no=concat(i1.item_no, '-', substr(i1.rightleft, 1,1))

编辑:如果行始终是连续的,并且您只需要选择而不是更新,则可以使用以下命令:

select
  i1.id,
  case when i1.description=i2.description or i2.id is null then i1.item_no else
            concat(i1.item_no, '-', substr(i1.rightleft, 1,1)) end,
  i1.description, i1.rightleft, i1.ref
from
  items i1 left join items i2
  on (i1.id=i2.id+1 or i1.id=i2.id-1) and (i1.item_no=i2.item_no)
order by i1.id

如果您使用的是mysql,则将依赖PHP循环;如果您使用的是Oracle或SQL Server,则可以对存储过程进行编程。

您的脚本应如下所示:

$dbh = new PDO('mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$data = $dbh->query("SELECT * FROM ExampleTable");
$dbh->beginTransaction();

foreach($data as $row)
{
    $append = $row["right\left"] == "left" ? $row["item_no"]."-l" : $row["item_no"]."-r";
    $stmnt = $dbh->prepare("UPDATE ExampleTable SET item_no = :item WHERE id = :id");
    $stmnt->execute(array(":item" => $append,":id" => $row["id"]));
}

// Do some exception handling if something goes wrong you can allways do a rollback
// With PDO $dbh->rollBack();
$dbh->commit();
$dbh = null;

尝试这个:

SELECT 
  id, 
  CASE RightLeft
    WHEN  'right' THEN CONCAT(item_no, '-r' )
    WHEN  'left'  THEN CONCAT(item_no, '-l' )
  END AS item_no,
  DESCRIPTION,
  Rightleft,
  ref
FROM Items
WHERE item_no IN
(
  SELECT i1.item_no
  FROM items i1
  GROUP BY i1.item_no
  HAVING(COUNT(DISTINCT description)) > 1);

SQL小提琴演示

这将为您提供:

| ID | ITEM_NO | DESCRIPTION | RIGHTLEFT | REF |
------------------------------------------------
|  4 |     3-r |          c1 |     right | ccc |
|  5 |     3-l |          c2 |      left | ccc |

像这样

UPDATE [dbo].[maTable] SET [item_no] = [item_no]+'r' WHERE not distinct [description] from [dbo].[maTable]

应在[说明]不相同的注册行中添加“ r”(为SQL Server编码)

暂无
暂无

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

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