VIEW中的SQL多项SELECT语句

时间:2018-06-30 10:37:54

标签: mysql sql

我正在从几个需要5列的表中创建一个视图。要创建每一列,我需要使用WHERE子句执行各种计算和异常。例如

if (progress.TotalBytesToReceive < 0 && progress.BytesReceived == 0)
{
    progressPercentage = progress.TotalBytesToSend < 0 ? 0 : progress.TotalBytesToSend == 0 ? 50 : (int)((50 * progress.BytesSent) / progress.TotalBytesToSend);
}
else
{
    progressPercentage = progress.TotalBytesToSend < 0 ? 50 : progress.TotalBytesToReceive == 0 ? 100 : (int)((50 * progress.BytesReceived) / progress.TotalBytesToReceive + 50);
}

此列工作完美,但现在我想在同一视图中的不同位置添加一列,其中带有不同的WHERE子句。如何添加几个SELECT语句,可以在其中分别指定每列的边界,还可以使用不同的表来做到这一点?

样本数据:

picture.replace('A_normal', 'A_400x400');

3 个答案:

答案 0 :(得分:2)

我认为您正在寻找GROUP BY子句以获取每年的利润:

CREATE VIEW test
AS SELECT Year(b.date) as 'YEAR', SUM(t.Profit)
   FROM booking b
       INNER JOIN Table2 t on b.BookingId=t.BookingId
   WHERE bookingDate BETWEEN '2010-01-01' and '2014-12-31'
   GROUP BY Year(date)

答案 1 :(得分:0)

在两个选择语句之间使用UNION,如下所示。

CREATE VIEW test
    AS SELECT date1 as 'YEAR' 
       FROM booking 
       WHERE bookingDate BETWEEN '2010-01-01' and '2014-12-31'
    UNION
       SELECT date2 as 'YEAR2' 
       FROM booking2 
       WHERE bookingDate BETWEEN '2010-01-01' and '2014-12-31'

答案 2 :(得分:0)

CREATE view test

    AS

    SELECT
        tst1.First,tst2.Second
        FROM (SELECT
                  date1 AS 'Year1',ROW_NUMBER() OVER(ORDER BY date1) AS RowNumber
                  FROM Table_1
                  WHERE  bookingDate BETWEEN '2010-01-01' and '2014-12-31'
             ) tst1
            LEFT OUTER JOIN (SELECT
                                 date1 AS 'Year2',ROW_NUMBER() OVER(ORDER BY date1) AS RowNumber
                                 FROM Table_2
                  WHERE  bookingDate BETWEEN '2010-01-01' and '2014-12-31'
                            ) tst2 ON tst1.RowNumber=tst2.RowNumber
    go