MySQL创建连接两个完整表的视图 [英] MySQL create view joining two whole tables

查看:1064
本文介绍了MySQL创建连接两个完整表的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个视图,合并来自两个不同表的不同所有列.

How can I create a view that merges different all columns from two different tables.

CREATE VIEW listView 
AS 
SELECT * FROM tab1 h LEFT JOIN tab2 b 
ON h.tID=b.tID 
WHERE value = 0

这给了我错误:

重复的列名"tID"

Duplicate column name 'tID'

是否有一种方法可以在不列出所有要选择的值的情况下联接两个表?

Is there a way to join the two tables without listing all the values to select?

推荐答案

两个表都包含列tID.为了编译VIEW,您需要在该列上创建一个别名,或仅指定一个tid和一个表,该别名将来自何处.

The two tables contains columns tID. In order to compile the VIEW, you need to create an alias on that column or just specify one tid and table where it will come from.

一种解决方案:

SELECT  h.TID, -- and not specifying b.TID
FROM    tab1 h LEFT JOIN tab2 b ON h.tID=b.tID 

另一种解决方案:提供别名

Another solution: supply an alias,

SELECT  h.TID as H_TID,
        b.TID as B_TID
FROM    tab1 h LEFT JOIN tab2 b ON h.tID=b.tID 

这篇关于MySQL创建连接两个完整表的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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