比较两行中的列以及另一列的不同更改值

时间:2012-12-11 14:31:22

标签: php mysql sql

我有一个带有自动增量键id的表,item_no可以是一行中的一行或两行(因此它们总是具有连续的id s)共享相同的行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

我的问题是,如果{匹配'行的item_no不同,我需要description将-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循环?

4 个答案:

答案 0 :(得分:2)

我会用这个:

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

答案 1 :(得分:1)

如果您使用的是mysql,我会依赖PHP循环,如果您使用的是Oracle或SQL服务器,那么您可以编写存储过程。

你的脚本应该是这样的:

$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;

答案 2 :(得分:1)

试试这个:

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 Fiddle Demo

这会给你:

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

答案 3 :(得分:0)

像这样的东西

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

应在[description]不相同的注册行中添加'r'(为SQL Server编码)