Mysql 为什么SQL视图没有显示联合查询的所有结果?

Mysql 为什么SQL视图没有显示联合查询的所有结果?,mysql,sql,Mysql,Sql,我创建了一个视图,其中有两个查询与一个UNION ALL连接在一起。如果在没有视图的情况下运行查询,它将显示所有预期结果,但如果运行视图,则不会显示所有预期结果。它显示运行视图的第一个查询的结果。为什么会这样 CREATE OR REPLACE VIEW V_TRAFFIC_ORDERS as "First query" select date.id date_id1, date.date date, channel.channel_name, product.product_name,

我创建了一个视图,其中有两个查询与一个UNION ALL连接在一起。如果在没有视图的情况下运行查询,它将显示所有预期结果,但如果运行视图,则不会显示所有预期结果。它显示运行视图的第一个查询的结果。为什么会这样

CREATE OR REPLACE VIEW V_TRAFFIC_ORDERS 
as
"First query"
  select date.id date_id1, date.date date, channel.channel_name, product.product_name, product.pay_type, orders.total, 
  orders.target, 
  0 as traffic, 
  0 as traffic_target /*Divided by 7 days to get a weekly target*/
  from orders
    inner join traffic on orders.date_id = traffic.date_id and orders.channel_id= traffic.channel_id
    inner join channel on channel.id = orders.channel_id
    inner join product on orders.product_id = product.id
    inner join date on date.id = orders.date_id
    left join traffic_target on traffic_target.week= date.week and 
    traffic_target.quarter= date.quarter and traffic_target.channel_id=orders.channel_id
UNION ALL
"Second query"
  select date.id date_id2, date.date date, channel.channel_name, product.product_name,product.pay_type, IFNULL(freesim.total,0) as orders, 0 as orders_target, uvs as traffic, IFNULL((traffic_target.target/7),0) as traffic_target
  from raw_coremetrics_page
    left join V_FREESIM_ORDERS as freesim on freesim.`date`= raw_coremetrics_page.date
    left join date on date.date=raw_coremetrics_page.date
    left join traffic_target on traffic_target.week= date.week and 
    traffic_target.quarter= date.quarter and traffic_target.channel_id=5
    inner join channel on channel.id = 5
    inner join product on product.id = 3
    where page like '%CHOOSER%' and category='CHOOSER'

请执行以下更改:

CREATE OR REPLACE VIEW V_TRAFFIC_ORDERS 
as
select * from (
  select date.id date_id1, date.date date, channel.channel_name, product.product_name, product.pay_type, orders.total, 
  orders.target, 
  0 as traffic, 
  0 as traffic_target /*Divided by 7 days to get a weekly target*/
  from orders
    inner join traffic on orders.date_id = traffic.date_id and orders.channel_id= traffic.channel_id
    inner join channel on channel.id = orders.channel_id
    inner join product on orders.product_id = product.id
    inner join date on date.id = orders.date_id
    left join traffic_target on traffic_target.week= date.week and 
    traffic_target.quarter= date.quarter and traffic_target.channel_id=orders.channel_id
UNION ALL
  select date.id date_id2, date.date date, channel.channel_name, product.product_name,product.pay_type, IFNULL(freesim.total,0) as orders, 0 as orders_target, uvs as traffic, IFNULL((traffic_target.target/7),0) as traffic_target
  from raw_coremetrics_page
    left join V_FREESIM_ORDERS as freesim on freesim.`date`= raw_coremetrics_page.date
    left join date on date.date=raw_coremetrics_page.date
    left join traffic_target on traffic_target.week= date.week and 
    traffic_target.quarter= date.quarter and traffic_target.channel_id=5
    inner join channel on channel.id = 5
    inner join product on product.id = 3
    where page like '%CHOOSER%' and category='CHOOSER')

请添加样本数据。嗨,Jens,谢谢你的兴趣,但这会很困难,因为这是机密数据。你是说你用机密数据开发?!?可怕的是…问题是,目前我们不知道您正在处理什么数据,也不知道此视图当前返回什么,也不知道您希望看到的“正确结果”是什么。尝试创建一个查询,第二个查询的结果可能在那里,但在结果集的末尾。或者,该视图可能仅使用查询的前半部分创建。谢谢Vinish,但这是不可能的,因为它说:视图的SELECT在FROM子句中包含一个子查询为每个子查询创建一个视图,然后使用union all子句从视图V_TRAFFIC_ORDERS中访问这些视图。耶!我这样做了,并且意识到带有视图的第二个查询没有得到结果。不知何故,这是由何处的一个条件造成的。我搬走了,然后开始工作。不知道,但它有效。谢谢,维尼什