从表中选择 [英] abt selecting from table

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

问题描述

我想知道是否有一种方法可以从表中选择值到X中,以便X可以保存多个值,以便用这些值更新另一个表,我正在做的是:
从acct中将数字选择为@number;
(此查询返回多个数字)
并显示此错误(这是预期的)"ERROR 1172(42000):结果由多个行组成"

当它满足特定条件时,如何使用此变量(@number)更新另一个表?


I want to know if there is a way to select values from table into X so that X can hold more than 1 value in order to update another table with those values , what i am doing is this :
select number into @number from acct;
(This query return more than 1 number )
and it show this error (which is expected) "ERROR 1172 (42000): Result consisted of more than one row"

How can i use this variable (@number) to update another table when it meet a certain criteria ?


plz help and thnx for advance.

推荐答案

如果您能够识别表中需要更新的记录,则可以使用使表与更新相关的任何键多行.

所以,
将Table1更新为T1,将Table2更新为T2设置T1.X = T2.X,其中T1.ID = T2.ID AND(唯一标识要更新的每一行所需的任何其他关系)

另外,如果您要执行插入操作而不是更新操作,则可以执行以下操作:

从表2的WHERE中插入表1(SELECT X,ID,OtherVariable1,OtherVariable1,DefaultValue)(满足某些条件));

SELECT语句中变量的顺序必须与要插入的表的模式匹配.另外,DefaultValue的意思是在SELECT语句内部,您可以为其中一列放置默认值.因此,如果列名为Active且其值为1或0,则只需将DefaultValue替换为1.

我希望这会有所帮助,尽管我不是100%地确定您的情况如何.如果您对我的问题没有足够清楚的说明,请在此问题上发表评论.另外,如果我完全不了解您的情况,可以请您发布每个表的架构以及您要执行的操作.
If you are able to identify which records need to be updated in the table you can use whichever keys make the tables relate to update multiple rows.

So,
UPDATE Table1 as T1, Table2 as T2 Set T1.X=T2.X WHERE T1.ID = T2.ID AND (Any other relationships required to uniquely identify each row you want to update)

Also, if you are doing an insert and not an update you are able to do:

INSERT INTO Table1 (SELECT X, ID, OtherVariable1, OtherVariable1, DefaultValue) FROM Table2 WHERE (certain conditions are met));

The order of the variables in the SELECT statment have to match the schema of the table you are inserting into. Also, what I meant by DefaultValue was that inside of the SELECT statement you could place a default value for one of the columns. So if the Column was called Active and it was either a 1 or 0 you could simply replace DefaultValue with 1.

I hope this helps although I am not 100% sure what your situation is. If you have any questions about something I didn''t clarify well enough please post them with a comment on this question. Also, if I missed the boat completely could you please post what your schemas are for each table and what you want to do.


这篇关于从表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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