结合VIEW和TABLE

时间:2013-12-03 10:47:08

标签: mysql

这些是我创建视图的代码对我来说很好,我只想添加一个字段名:job_type,它来自一个名为Job Type的表。

所以基本上,我只想结合使用VIEW: vinvoices 和TABLE: tjobtypes

我的代码:

CREATE VIEW vinvoicesbymthbyjob 
as 
select 
`vinvoices`.`yymm` AS `yymm`,
`vinvoices`.`location` AS `job_location`,
`vinvoices`.`job_no` AS `job_no`,
format(sum(`vinvoices`.`amount`),0) AS `amount` from `vinvoices` 

TJOBTYPES

  • type_no
  • TYPE_NAME

TJOBS

  • job_no
  • job_name
  • value
  • start_date
  • completion_date
  • duration_mths
  • type_no
  • manager_no
  • client_no
  • location
  • 状态
  • 注释

VINVOICES

  • id
  • 日期
  • job_no
  • JOB_NAME
  • CLIENT_NAME
  • 金额
  • ppaid
  • inv_no
  • 概率
  • 注释
  • 接触
  • 位置
  • YYMM

提前致谢! :)

1 个答案:

答案 0 :(得分:0)

正如我在之前的评论中所说,你必须加入表格。 Here is the documentation。视图只不过是一个查询,但它可以像表一样使用:

SELECT 
    vinvoices.yymm AS yymm,
    vinvoices.location AS job_location,
    vinvoices.job_no AS job_no,
    format(sum(vinvoices.amount),0) AS amount,
    tjobtypes.type_name AS job_type
FROM vinvoices 
JOIN tjobs ON tjobs.job_no = vinvoices.job_no
JOIN tjobtypes ON tjobtypes.type_no = tjobs.type_no