我想创建一个数据库视图。 \实际上,我的数据库包含大约20个表,我想基于外键加入表,但有一些重复列。有一种简单快捷的方法吗?
答案 0 :(得分:0)
你应该像这样使用Alias ......
Select E.Name , U.Name,E.Age,U.Age
From
TblEmployee E
inner join
TblUser U on E.EmployeeId = U.EmployeeId
注意:以上查询只是一个示例,以便您可以继续
修改:您也可以按照这个名称进行列名称
Select E.* , U.*, E.Age as Employee Age ,U.Age as UserAge
/* you can specify some column by E.Name and if you do not want then simplye do E.* by doing thing every column of your that table will be selected */
From TblEmployee E
inner join
TblUser U on E.EmployeeId = U.EmployeeId
答案 1 :(得分:0)
确定您在视图中需要哪些列,并使用AS
为所有列指定特定名称。
这是一个例子。两个表都有col1和col2:
MySQL 5.5.32架构设置:
CREATE TABLE Table1
(`Id` int, `Col1` varchar(1), `Col2` varchar(1), `Id2` int)
;
INSERT INTO Table1
(`Id`, `Col1`, `Col2`, `Id2`)
VALUES
(1, 'A', 'B', 1),
(2, 'B', 'C', 2),
(3, 'D', 'E', 3)
;
CREATE TABLE Table2
(`Id` int, `Col1` varchar(1), `Col2` varchar(1))
;
INSERT INTO Table2
(`Id`, `Col1`, `Col2`)
VALUES
(1, 'A', 'B'),
(2, 'B', 'C'),
(3, 'D', 'E')
;
查询1 :
SELECT Table1.Id as ID,
Table1.Col1 as COL1,
Table1.Col2 as COL2,
Table2.Col1 as COL3,
Table2.Col2 as COL4
FROM Table1
INNER JOIN Table2 ON Table1.Id2 = Table2.Id
<强> Results 强>:
| ID | COL1 | COL2 | COL3 | COL4 |
|----|------|------|------|------|
| 1 | A | B | A | B |
| 2 | B | C | B | C |
| 3 | D | E | D | E |