gpt4 book ai didi

mysql - 合并两个查询但不是垂直的水平查询

转载 作者:行者123 更新时间:2023-11-29 02:57:51 26 4
gpt4 key购买 nike

我想将两个查询联合为垂直的,所以通常不使用联合参数。

select 
Code, Name, Category, SubCategory, p.Brand, Model, Price, Currency,
Unit, Template, F1, F2,P1, P2, DiscountPercent as PrivateDiscount from
Products as p
left join ProductTemplates as pt on pt.TemplateName=p.Template
left join Discounts as d
on d.Brand = p.Brand and d.Status=1 and d.CustomerID='1198' and d.DiscountType=0

select DiscountPercent as PublicDiscount
from Products as p
left join ProductTemplates as pt on pt.TemplateName=p.Template
left join Discounts as d
on d.Brand = p.Brand and d.Status=1 and d.DiscountType=1

输出:查询1

colum name  | Code  | Name  | Category  | Brand  | PrivateDiscount |
1.row _____ | ssff3 | adcdd | affcdcddd | assfdd | %6 |
2.row _____ | scf3 | adcdd | affcdcddd | assfdd | %6 |

输出:查询2

colum name | PublicDiscount |
1.row _____| %2 |
2.row _____| %3 |

我想要这个结果:

colum name : | Code  | Name  | Category  | Brand  | PrivateDiscount | PublicDiscount |
1.row _____ | ssff3 | adcdd | affcdcddd | assfdd | %6 | %2 |
2.row _____ | scf3f | adcdd | affcdcddd | assfdd | %6 | %3 |

因此,应该是“PublicDiscount”列,紧挨着“PrivateDiscount”列怎么能这样呢?

最佳答案

只需加入两次折扣表:

select 
Code, Name, Category, SubCategory, p.Brand, Model, Price, Currency,
Unit, Template, F1, F2,P1, P2,
privd.DiscountPercent as PrivateDiscount,
pupld.DiscountPercent as PublicDiscount
from Products as p
left join ProductTemplates as pt on pt.TemplateName=p.Template
left join Discounts as privd on privd.Brand = p.Brand and privd.Status=1 and privd.CustomerID='1198' and privd.DiscountType=0
left join Discounts as pupld on pupld.Brand = p.Brand and pupld.Status=1 and pupld.DiscountType=1;

关于mysql - 合并两个查询但不是垂直的水平查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28560887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com