从同一个表中加入多个Sql语句

时间:2018-03-21 10:11:39

标签: mysql sql database-design pivot relational-database

我已经阅读了很多关于这个主题的帖子,但我还不满意。

我有一个名为ticket的表,其中包含以下列

TicketID | AirlineID | PassengerID | TicketPrice | TicketVolume | DestinationCountry | ExitCountry | TicketDate`

我有多个查询,例如

SELECT AVG(TicketPrice) 
FROM ticket 
WHERE TicketPrice between 552 and 1302
AND AirlineID=1 
AND TicketDate between '2016-01-01' and '2016-12-31' 
GROUP BY TicketDate

SELECT AVG(TicketPrice) 
FROM ticket 
WHERE TicketPrice between 552 and 1302 
AND AirlineID=1 
AND TicketDate between '2017-01-01' and '2017-12-31' 
GROUP BY TicketDate

请问如何将两个查询同时加入另一个表格

+--------------------------------++-----------
| AirlineID || Average Ticket Price 2016/2017|
+--------------------------------++-----------

他们实际上是更多的疑问。

4 个答案:

答案 0 :(得分:5)

只需使用CASE即可实现此目的:

试试这个:

SELECT
    AirlineID,
    AVG(CASE WHEN TicketDate BETWEEN '2016-01-01' AND '2016-12-31' THEN TicketPrice END),
    AVG(CASE WHEN TicketDate BETWEEN '2017-01-01' AND '2017-12-31' THEN TicketPrice END) 
FROM ticket 
WHERE
    TicketPrice BETWEEN 552 AND 1302 AND
    AirlineID = 1 
GROUP BY
    AirlineID, TicketDate;

答案 1 :(得分:2)

这是我的解决方案,也许不是OP的理想输出,但在我看来,输出更好,也不需要硬编码日期

livy.spark.master=yarn
livy.spark.deploy-mode = cluster
livy.server.recovery.mode = recovery
livy.server.recovery.state-store = zookeeper
livy.server.recovery.state-store.url = localhost:2181

答案 2 :(得分:1)

如果要将结果合并到单个表中,请使用UNION。

SELECT {...}
UNION
SELECT {...}

SELECT语句必须返回相同的列

答案 3 :(得分:0)

<强> SQL

import UIKit

class pictureCell: UITableViewCell {

    @IBOutlet weak var pictureImageView: UIImageView!
    @IBOutlet weak var pictureTitleLabel: UILabel!

    func setPicture(picture: picture) {
        pictureImageView.image = picture.image
        pictureTitleLabel.text = picture.title
    }

}