假设我有2张桌子
出售
ProductName SellingDate ProductID
iPhone 4G 2011-05-11 1
iPhone 4S 2011-05-12 2
iPhone 4S 2011-05-13 2
iPhone 4G 2011-05-15 1
求购
ProductName BuyingDate ProductID
iPhone 4S 2011-05-09 2
iPhone 4G 2011-05-10 1
iPhone 4G 2011-05-13 1
iPhone 4G 2011-05-14 1
如何将此日期合并为此?
ProductName Date Status ProductID
iPhone 4G 2011-05-10 Buying 1
iPhone 4G 2011-05-11 Selling 1
iPhone 4G 2011-05-13 Buying 1
iPhone 4G 2011-05-14 Buying 1
iPhone 4G 2011-05-15 Selling 1
我不知道如何将2个表日期(SellingDate / BuyingDate)组合成1个表(日期)......
任何帮助都会得到赞赏!!
答案 0 :(得分:1)
我假设你想要的输出与你给定的输入不匹配,所以如果这只是一个组合两个表的情况,那么UNION ALL
就足够了。
SELECT ProductName, Date, 'Buying' AS Status, ProductID FROM Buying
UNION ALL SELECT ProductName, Date, 'Selling' AS Status, ProductID FROM Selling
您可以使用以下内容添加用户可选择的productid
SELECT ProductName, Date, 'Buying' AS Status, ProductID FROM Buying WHERE ProductID = 1
UNION ALL SELECT ProductName, Date, 'Selling' AS Status, ProductID FROM Selling WHERE ProductID = 1
或者
SELECT *
FROM (
SELECT ProductName, Date, 'Buying' AS Status, ProductID FROM Buying
UNION ALL SELECT ProductName, Date, 'Selling' AS Status, ProductID FROM Selling
) p
WHERE ProductID = 1
如果您确实需要输入那些输出,则必须向我们提供一些其他信息
作为附注并供将来参考,请尽可能准确和完整。我认为您的投票是由于您的输出与您的输入不匹配而导致的。