如何编写一个选择查询以选择两个表列? [英] how to write a single select query for selecting two table columns?
问题描述
先生,
我正在使用两个表,即table1和table2.如果我执行table1结果是
从表1中选择*
hi sir,
i am taking two tables i.e., table1 and table2. if i execute table1 result is
Select * from table1
Name class
Ajay 8
raju 9
Surya 7
ajay raju 10
并执行table2
从表2中选择*
and execute table2
select * from table2
Dept Gen
Accounts M
Management F
Research M
Developing M
designing M
我想从table2中提取部门列
我写类似
的查询
从table1,table2中选择dbo.table1.name,dbo.table1.ID,dbo.table2.dept
那么结果是:
i want to take dept column from table2
that i write query like
select dbo.table1.name,dbo.table1.ID,dbo.table2.dept from table1,table2
Then result is:
Name ID Dept
Ajay 8 Accounts
Ajay 8 Management
Ajay 8 Research
Ajay 8 Developing
Ajay 8 designing
Raju 9 Accounts
Raju 9 Management
Raju 9 Research
Raju 9 Developing
Raju 9 designing
Surya 7 Accounts
Surya 7 Management
Surya 7 Research
Surya 7 Developing
Surya 7 designing
Ajay raju 10 Accounts
Ajay raju 10 Management
Ajay raju 10 Research
Ajay raju 10 Developing
Ajay raju 10 designing
这次将结果显示为次数.
但我的目标是像这样显示结果:
this time it display result as number of times.
but my aim is result display like:
Name ID Dept
Ajay 8 Accounts
raju 9 Management
Surya 7 Research
ajay raju 10 Developing
null null designing
请给出一个解决方案.并为此提供一个示例选择查询
谢谢您,先生.
Please give a solution. and please give a sample select query for this
Thank you sir.
推荐答案
在您提出完整的问题之前,任何人都很难帮助您.
根据您在这里的要求,table1和amp;没有公共列的单个查询中的table2:
It''s really difficult for anyone to help you until you present the full problem out here.
Based on just what you have asked here, everything from both the table1 & table2 in a single query that has no common column:
Select * From Table1, Table2
嗨Ajay,
无法获得输出.您可以在两个表中都提供任何公共列,这是可能的.
例如,"ID"是两个表中的公共列,然后使用以下查询
从表1 T1,表2 T2中选择T1.ID,T1.Name,T2.Dept,其中T1.ID = T2.ID
(或)
从Table1上的Table1内部联接Table2中选择Table1.ID,Table1.Name,Table2.Dept
干杯:)
Hi Ajay,
Its not possible to get your output.You can give any common column in both the table means is possible.
For example ''ID'' is the common column in two tables then use the following query
select T1.ID,T1.Name,T2.Dept from Table1 T1,Table2 T2 where T1.ID=T2.ID
(or)
select Table1.ID,Table1.Name,Table2.Dept from Table1 inner join Table2 on Table1.ID=Table2.ID
Cheers :)
这篇关于如何编写一个选择查询以选择两个表列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!