如何通过键列(用于联接表)联接表? [英] How to join tables by the key columns (used to join tables)?

查看:151
本文介绍了如何通过键列(用于联接表)联接表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,表A有两列,主键列名为"MS",另一列名为"Name",表B有2列,主键列名为"MS",其他列名为电话".

我可以使用左连接...或简单地使用"where"将两个表连接起来,但是所有这些方式还返回一对在同一条记录上显示相同值的列,即A.MS和B.MS,这不好外观,特别是当我从联接表中使用"select *"时,它将显示两个列,除非我必须从联接表中选择想要的每个列,例如"select A.MS,Name,Phone". (如果使用select *,则看起来像"A.MS,名称,B.MS,电话",这是联接表的列从左到右的顺序).实际上,我有许多表包含许多列,并且我想将它们全部合并成一个表.如果使用"select *",则简单但出乎意料的是,另一种方法太繁琐,因为要连接的表数量很大!有什么办法可以毫不费力地做到吗?
非常感谢!

I have 2 tables, the table A with two columns, the primary key column named "MS", the other column named "Name", the table B has 2 columns, the primary key column named "MS", the other column named "Phone".

I can join two these tables using left join ... or simply "where" but all such ways also return a couple of column displaying the same value at the same record, that is A.MS and B.MS, that is not good to look, and especially, when I use "select *" from the joined table, it will show two those column unless I have to select each column I want like as "select A.MS, Name, Phone" from the joined table. (if using select *, it looks like "A.MS, Name, B.MS, Phone" as the order from left to right of the columns of the joined table). I have, in fact, many tables with many columns, and I want to join all them into an only table. If using "select *", simply but unexpectedly, the other way is too tedious to do because of the large number of tables to be joined! Is there any way to do it effortlessly?
Thank you so much!

推荐答案

老实说,只是不要使用select *.如果使用select *,将获得所有列.如果只需要一些列,则不要使用select *.
Honestly, just don''t use select *. If you use select * you will get all columns. If you only want some columns, then don''t use select *.


在SQL中,您只有很少的选择:
-使用*即可获取所有内容
-使用别名.*从表中获取所有内容(可能与其他表的其他列一起使用)
-定义要在结果中包含的每一列.
正如您已经注意到的,您可以混合使用选项2和3.
In SQL you have only few options:
- use * to get everything
- use alias.* the get everything from a table (possibly with other columns for other tables)
- define each column you want to have in the result, one by one.
As you have already noticed you can mix option 2 and 3.


这篇关于如何通过键列(用于联接表)联接表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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