MYSQL:根据唯一行值向表中添加唯一数值?

时间:2017-10-14 19:34:14

标签: mysql

我有这个表,我想把它分成三个表,所以我有名字,名称和订单之间的关系表,第三个只包含订单。所以我认为一个简单的方法是在这里添加一个唯一的数字列

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js"></script>

<textarea id="bar1">Some content 1</textarea>
<button class="clipboard-btn1" data-clipboard-action="copy" data-clipboard-target="#bar1">
  Copy to clipboard
</button>
<br>
<textarea id="bar2">Some content 2</textarea>
<button class="clipboard-btn2" data-clipboard-action="copy" data-clipboard-target="#bar2">
  Copy to clipboard
</button>
<br>
<textarea id="bar3">Some content 3</textarea>
<button class="clipboard-btn3" data-clipboard-action="copy" data-clipboard-target="#bar3">
  Copy to clipboard
</button>
<br>

我想添加一个数字列,其中包含与唯一+----+------+-----------------------+-------+--------------+ | id | name | address | phone | order_number | +----+------+-----------------------+-------+--------------+ | 1 | Joe | Joes Address | 1111 | 1390842 | | 2 | Paul | Pauls Address | 2222 | 9082309 | | 3 | Greg | Gregs Address | 3333 | 0928340 | | 4 | Lucy | Lucys Address | 4444 | 9028340 | | 5 | Paul | Pauls Address | 2222 | 8958399 | | 6 | Tom | Toms Address | 5555 | 9084024 | | 7 | Lucy | Lucys Another Address | 4444 | 9801983 | | 8 | Paul | Pauls Another Address | 2222 | 0982304 | +----+------+-----------------------+-------+--------------+ 值相关联的增量编号,以便预期结果为

name

我该怎么办?

2 个答案:

答案 0 :(得分:1)

使用用户定义的变量

与您想要的结果集相似的一些内容
public void method() {
    // declare variables...

    if (Character.toUpperCase(choosePassOverPed) == 'Y') {
        passengerPoints = passengerPoints + toughCallThresholdPoints;
        checkPoints(passengerPoints, pedestrianPoints, numAdultPedestrians, numChildPedestrians, numAdultPassengers, numChildPassengers);
    } else {
        pedestrianPoints = pedestrianPoints + toughCallThresholdPoints;
        checkPoints(passengerPoints, pedestrianPoints, numAdultPedestrians, numChildPedestrians, numAdultPassengers, numChildPassengers);
    }
}

private static void checkPoints(int passengerPoints, int pedestrianPoints, int numAdultPedestrians, int numChildPedestrians, int numAdultPassengers, int numChildPassengers) {
    if (passengerPoints > pedestrianPoints) {
        System.out.println("Fatality Group: PEDESTRIANS");
        log(numAdultPedestrians, numChildPedestrians);
    } else {
        System.out.println("Fatality Group: OCCUPANTS");
        log(numAdultPassengers, numChildPassengers);
    }
}

private static void log(int numAdultPedestrians, int numChildPedestrians) {
    System.out.printf("Number of adult fatalities: %d%n", numAdultPedestrians);
    System.out.printf("Number of child fatalities: %d%n", numChildPedestrians);
}

DEMO

另一个简单版本,假设id列设置为自动递增,如果是,那么您可以使用相关子查询来选择相同名称记录的最小ID

select `id`, `name`, `address`, `phone`, `order_number`,
@b:= case when `name` <> @a then @b + 1 else @b end NID,
@a:= `name`
from (
    select *
    from demo b,
    (select @a:=null,@b:=1) a
    order by name 
) c
  

上面的注释不保证它完全依赖于id列值的序列

DEMO

答案 1 :(得分:0)

如果您只是想显示NID,那么@M Khalid Junaid的答案将会正常运作。

但是如果你想在表格中添加列NID,那么以下查询将完成工作:

alter table t
add column nid integer;

update t
set nid = (Select 
           (case when count(name)>1 then min(id)
                 else  id
            end)
           from
           (select *from t) x
           where t.name = x.name          
           );

注意:Nid不包含增量序列。它基于id列。

希望它有所帮助!