更新问题 [英] Problem for Update

查看:87
本文介绍了更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个表行(ID,LineNo,LineName),其值为:



I have a table line (ID, LineNo, LineName) having values as :

1, 1, <blank>
2, 2, <blank>
3, 3, <blank>


现在,我必须编写一个查询,以使其更新来自空白表(如上显示为< blank>)的行表中的LineName列,其值来自表linet(linenamet)的值为:


Now i have to write a query such that it updates LineName column from line table which is blank(as shown above with <blank>) with values from table linet(linenamet) having values as :

a
b
c

我的试用查询是:
更新行集LineName =(从linet中选择linenamet);

它给出错误为:子查询返回多于1行"

预期结果:
更新后,它应将行表显示为:

My trial query is :
update line set LineName = (select linenamet from linet);

It gives error as : "Subquery returns more than 1 row"

Expected Result:
After Updation it should show line table as:

1, 1, a
2, 2, b
3, 3, c


请让我知道相同的更新SQL查询.

感谢和问候,


Kindly let me know the update SQL query for the same.

Thanks and Regards,

推荐答案

以下查询您所写的内容将导致您明确指出的错误,因为从linet中获取的行数多于一个linenamet,对于你就是例子.

更新行集LineName =(从linet中选择linenamet);

为此,您可以像这样修改查询.

Following query what you''ve written will cause error you stated definately because you are getting more then one linenamet from linet, That is 3 for you example.

update line set LineName = (select linenamet from linet);

For that you can modify your query like.

update line set LineName = (select linenamet from linet --include some condition which gives you exact single result from corrosponding table)



如果linenamet表中只有一列,那么我认为是无法获取的.



If there''s a single column in linenamet table then I think it is not possible to fetch.


如果您的表行也有一个主键来链接它(外部键)到表行中的ID或LineNo,它看起来像这样
linet中名为ID的列是引用行列ID的外键:
If your table linet also had a primary key to it that would link it (foreign key) to a either ID or LineNo in table line it would look like this
Column named ID in linet is foreign key referencing line column ID:
UPDATE line SET LineName = (SELECT linenamet FROM linet ln WHERE ln.ID = line.ID)


linet中名为ID的列是引用表行列LineNo的外键:


Column named ID in linet is foreign key referencing table line column LineNo:

UPDATE line SET LineName = (SELECT linenamet FROM linet ln WHERE ln.LineNo = line.ID)


由于我们不知道表linet是否具有任何外键或您根本不需要告诉我们的任何键.也许有第三个表使表之间的关系为线和线.

最好的问候,
曼弗雷德(Manfred)


Since we don''t know if table linet has any foreign key or any key at all you need to tell us. Maybe there is a third table that makes the relation between the tables linet and line.

Best Regards,
Manfred


这篇关于更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆