创建后视图以组合多个表数据

发布于 2025-02-12 01:25:22 字数 1054 浏览 1 评论 0原文

我有多个具有不同列名称的表,如下,

客户表: c_id, customer_name, phone_number, postal_code

员工表:员工_name,e_id,mobile_no,zip_code

学生表:s_id,student_name,post_code

我需要创建视图来组合诸如下面的

用户表<>用户表<> /strong>:userId(c_id,e_id,s_id),用户名(所有名称列),zip_code,contact

示例数据

客户表:

|  c_id  |  Company_Name  |  Phone |
|----------------------------------|
|  1     |  Company 1     | *******|
|----------------------------------|

员工表:

|  e_id  |  Employee_Name | Mobile |
|----------------------------------|
|  1     |  employee 1    | *******|
|----------------------------------|

期望查看:

|  userId  |   user_Name   | contact  |
|-------------------------------------|
|  c_1     |   Company 1   |  ******* |
|-------------------------------------|
|  e_1     |   employee 1  |  ******* |
|-------------------------------------|

如何创建视图,请帮助我,提前致谢

I have multiple tables with different column names like below,

customer table:
c_id,
customer_name,
phone_number,
postal_code

Employee Table: Employee_name, e_id, mobile_no,zip_code

student Table: s_id, student_name, post_code

I need to create view to combine those tables like below

User Table: userId (c_id, e_id, s_id), userName (all name columns), zip_code, contact

Sample Data

Customer Table:

|  c_id  |  Company_Name  |  Phone |
|----------------------------------|
|  1     |  Company 1     | *******|
|----------------------------------|

Employee Table:

|  e_id  |  Employee_Name | Mobile |
|----------------------------------|
|  1     |  employee 1    | *******|
|----------------------------------|

Expected View:

|  userId  |   user_Name   | contact  |
|-------------------------------------|
|  c_1     |   Company 1   |  ******* |
|-------------------------------------|
|  e_1     |   employee 1  |  ******* |
|-------------------------------------|

how to create the view please help me, Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2025-02-19 01:25:22

只需使用Union联合所有根据重复值。基于数据

CREATE VIEW view_name
AS
  SELECT concat('c_',c_id)         AS userId,
         company_name AS user_Name,
         phone        AS contact
  FROM   customer
  UNION
  SELECT concat('e_',e_id),
         employee_name,
         mobile
  FROM   employee   

a href =“ https://dbfiddle.uk/?rdbms = postgres_10&; fiddle = 88f7473090fc386cdc666666dfbd30c37111”

<

CREATE VIEW view_name
AS
  SELECT c_id          AS userId,
         customer_name AS userName,
         postal_code   AS zip_code,
         phone_number  AS contact
  FROM   customer
  UNION
  SELECT e_id,
         employee_name,
         zip_code,
         mobile_no
  FROM   employee
  UNION
  SELECT s_id,
         student_name,
         post_code,
         NULL
  FROM   student  

just use Union or Union All depending on duplicate values. based on data

CREATE VIEW view_name
AS
  SELECT concat('c_',c_id)         AS userId,
         company_name AS user_Name,
         phone        AS contact
  FROM   customer
  UNION
  SELECT concat('e_',e_id),
         employee_name,
         mobile
  FROM   employee   

dbfiddle

based on tables

CREATE VIEW view_name
AS
  SELECT c_id          AS userId,
         customer_name AS userName,
         postal_code   AS zip_code,
         phone_number  AS contact
  FROM   customer
  UNION
  SELECT e_id,
         employee_name,
         zip_code,
         mobile_no
  FROM   employee
  UNION
  SELECT s_id,
         student_name,
         post_code,
         NULL
  FROM   student  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文