gpt4 book ai didi

MySQL,两个表显示来自两个表的信息

转载 作者:行者123 更新时间:2023-11-29 04:43:24 25 4
gpt4 key购买 nike

我目前正在学习 SQL,我有一个问题。我有两张 table ,分别叫做“vehicul”和 proprietate。第一个表(车辆)如下所示:

   id   nr_vehicul  marca   id_marca    tip     culoare     capacitate_cilindrica
1 DJ-01-AAA Mercedes 1 CLK 350 negru 3500
2 DJ-01-BBB Mercedes 1 S 500 silver 5000
3 DJ-01-CCC Mercedes 1 ML 550 alb 5500
4 DJ-01-DDD BMW 2 325 galben 2500
5 DJ-01-EEE BMW 2 X5 negru 350

第二个表(专有)如下所示:

id serie_buletin    cnp        nr_vehicul data_cumpararii pret
1 AK162332 2006036035087 DJ03AAA 2014-05-01 35000
2 AK162332 2006036035087 DJ03BBB 2014-05-02 90000
3 AK176233 6548751520125 DJ03CCC 2014-05-03 55000
4 BZ257743 6548751520125 DJ03DDD 2014-05-04 25000
5 BZ257743 2006036035087 DJ03EEE 2014-05-05 63000

我想显示第一个表中的“marca”列和第二个表中的“price”列,但像这样。

 marca   |  pret
Mercedes | 180000
BMW | 88000

基本上我有三辆梅赛德斯车和两辆宝马车,我怎么能显示一次梅赛德斯和这三辆汽车的价格总和?

最佳答案

您需要连接两个表并根据 marca 字段对它们进行 GROUP 并求和 pret

select marca, sum(pret)
from table1 as t1, table2 as t2
where t1.id=t2.id
group by marca

这里我假设 id 字段正在连接两个表,(但正如我从您的样本数据中看到的那样,它实际上并不相互关联)

编辑

我认为您在 table2 中缺少 id_marca 字段。如果它在那里,那么它将加入该列,如下例所示:

select marca, sum(pret)
from table1 as t1, table2 as t2
where t1.id_marca=t2.id_marca
group by id_marca;

关于MySQL,两个表显示来自两个表的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23671959/

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