-1

我有 3 个数据库:service1、businessrecord 和 teamAstaffno

1)我想在“play”和“mainbusiness”和“error”两种类型中识别服务类型

-if businessrecord.type like "play" as "play"

-if businessrecord.other 匹配 service1.servicetype 为“mainbusiness”

-如果它满足两个选项应该显示“错误”

2)我只想要匹配teamAstaffno.staffno的业务记录,因为我只想要A队记录。

3)最后,我想要一个具有 cloumn order_type 的表来显示“play”、“mainbusiness”、“error”和“duplicate”

比,什么将是 sql 代码?

我这样打字

    select businessrecord.type, businessrecord.other, businessrecord.staffno;
from service1;
    join  businessrecord;
        on businessrecord.other = service1.servicetype;
    inner join teamAstaffno;
        on businessrecord.staffno = teamAstaffno.staffno

并将记录复制到表 proc1

alter table proc1 add order_type Char(50) 
update order_type with "mainbusiness" where businessrecord.type like service1.servicetype
update order_type with "duplicate" where order_type like "mainbusiness" and type like "play"
update order_type with "play" where other like "play" 
update order_type with "Error" where order_type is null

我究竟做错了什么?

如果使用两个表,我应该使用连接函数吗?代码如何?

4

2 回答 2

1

你的UPDATE语法不正确

UPDATE <tablename>
SET <columnname> = <somevalue>
WHERE <predicate>

一些 dbms' 让你为你的更新做一个连接

UPDATE alias1
SET alias1.column = 'somevalue'
FROM table1 alias1
JOIN table2 alias2 ON alias1.ID = alias2.ForeignID
WHERE alias2.SomeColumn = 'blah'
于 2013-10-07T04:02:55.310 回答
0

如果你需要在 WHERE 语句中加入,你可以这样做:

UPDATE     t1
SET        Col2 = t2.Col2,
           Col3 = t2.Col3,
           Col4 = t2.Col4,
FROM       Table1 t1
INNER JOIN Table2 t2 ON t1.Col1 = t2.Col1
WHERE      t1.Col1 = 'whatever' 
AND        t2.Col3 = 'somethingelse'

所以在你的情况下,我猜它是这样的:

UPDATE      service1
SET         OrderType = (CASE 
                WHEN businessrecord.type = service1.servicetype THEN 'mainbusiness'
                WHEN order_type = 'mainbusiness' and type = 'play' THEN 'duplicate'
                WHEN other = 'play' THEN 'play'
                WHEN order_type IS NULL THEN 'Error'
            END)
FROM        service1
INNER JOIN  businessrecord
ON          businessrecord.other = service1.servicetype
INNER JOIN  teamAstaffno
ON          businessrecord.staffno = teamAstaffno.staffno

注意:这可能不是您想要的,但至少它向您展示了如何使用CASE语句进行条件更新。

请参阅此处的 MSDN 文档:http ://technet.microsoft.com/en-us/library/ms181765.aspx

于 2013-10-07T07:03:56.790 回答