如何使用给定查询从4个表中选择唯一列名 [英] how to select the unique column names from 4 tables using the given query

查看:59
本文介绍了如何使用给定查询从4个表中选择唯一列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 选择 *
来自 t1
其中 存在(选择 * 的代码 t2  and  t2.col2 = t1.col2)




如何修改四个表的相同查询

请给我一个帮助.他们.像这样的东西:

  SELECT  t1.Col1  AS  T1_col1,
       t1.Col2  AS  T1_col2,
       t2.Col1  AS  T2_col1,
       t2.Col2  AS  T2_col2,
       ...
 FROM  Table1 t1  INNER   JOIN  Table2 t2 打开 t1.PrimaryKeyCol = t2.ForeignKeyCol
      ...
位置 ... 


如果您想要唯一的名称,则不能使用select *代替tablename. fieldName作为新名称

 选择 t1.FName  as  f1,
t2.FName  as  f2,
t3.FName  as  f3,
t4.FName  as  f4

来自 t1 内部 加入 t2 上t1.col = t2.col
内部 加入 t3  on  t2.col = t3.col
内部 加入 t4  on  t3.col = t4.col
其中 ... 


select *
from t1
where exists(select * from t2 where t2.col1 = t1.col1 and t2.col2 = t1.col2)




how to modify same query for four tables

Please give me a Help

解决方案

If your question is about the tables having same column names and you want to rename them in the result set, then you should define aliases for them. Something like:

SELECT t1.Col1 AS T1_col1,
       t1.Col2 AS T1_col2,
       t2.Col1 AS T2_col1,
       t2.Col2 AS T2_col2,
       ...
FROM  Table1 t1 INNER JOIN Table2 t2 ON t1.PrimaryKeyCol =  t2.ForeignKeyCol
      ...
WHERE ...


if you want unique name than you could not use select * instead of use tablename.fieldName as newname

select t1.FName as f1,
t2.FName as f2,
t3.FName as f3,
t4.FName as f4

from t1 inner join t2 on t1.col=t2.col 
inner join t3 on  t2.col=t3.col 
inner join t4 on  t3.col=t4.col 
where ...


这篇关于如何使用给定查询从4个表中选择唯一列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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