在mysql查询中选择查询作为vaule [英] Select query as a vaule in mysql query

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

问题描述

我有两个表tb1和tb2



tb1中有四列tb1c1,tb1c2,tb1c3,tb1c4



和tb2中的三列



tb2c1,tb2c2,tb2c3



当我是在tb1中插入值然后我想将tb2c2包含为tb1c3字段中的值



我虽然做了类似的事情:



 插入 进入 tb1(tb1c1,tb1c2, tb1c3,tb1c4)'  1''  2'选择 tb2c2 fromtb2 其中 tb1c1 = 1)





请在此查询中帮助我



我尝试过的事情:



我有两张桌子tb1和tb2



ther e是tb1中的四列tb1c1,tb1c2,tb1c3,tb1c4



和tb2中的三列



tb2c1,tb2c2,tb2c3



当我将值插入tb1然后我想将tb2c2含量作为值插入tb1c3字段



我虽然做了类似的事情:



  insert   into  tb1(tb1c1,tb1c2,tb1c3,tb1c4) values '  1''  2'选择 tb2c2 fromtb2 其中 tb1c1 = 1)





请在这个查询中帮助我

解决方案

Digimanus给了你一个方法,我个人认为这是一个糟糕的格式。我建议做类似但是这样:

insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4)
SELECT ' 1'' 2',tb2c2,someValue
来自 tb2
其中 tb1c1 = 1





注意你仍然需要tblc4的东西,但你发布的代码中没有任何内容。


I have two tables tb1 and tb2

there are four column in tb1 tb1c1,tb1c2,tb1c3,tb1c4

and three column in tb2

tb2c1,tb2c2,tb2c3

when i am inserting values into the tb1 then I want tb2c2 containt as value into the tb1c3 field

I though to do something like follows:

insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4) values('1','2',select tb2c2 fromtb2 where tb1c1=1)



please help me in this query

What I have tried:

I have two tables tb1 and tb2

there are four column in tb1 tb1c1,tb1c2,tb1c3,tb1c4

and three column in tb2

tb2c1,tb2c2,tb2c3

when i am inserting values into the tb1 then I want tb2c2 containt as value into the tb1c3 field

I though to do something like follows:

insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4) values('1','2',select tb2c2 fromtb2 where tb1c1=1)



please help me in this query

解决方案

Digimanus gave you one way, personally I think that is a bad format to do it in. I suggest doing it similar but like this:

insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4) 
SELECT '1','2', tb2c2, someValue
from tb2 
where tb1c1=1



Note you still need something for tblc4 but you didn't have anything in your code you posted.


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

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