0

如果我可以帮助以下代码,我将需要:

CREATE TABLE `car` (                                     
          `car_id` int(10) unsigned NOT NULL auto_increment,      
          `car_name` varchar(25) NOT NULL,                    
          `car_year` date NOT NULL,                       
          PRIMARY KEY  (`user_id`)                                 
) ENGINE=InnoDB;
CREATE TABLE `people` (                                                                                              
             `peo_id` int(10) unsigned NOT NULL auto_increment,                                                                    
             `peo_name` varchar(50) NOT NULL,                                                                                      
             `peo_surname` varchar(200) NOT NULL,                                                                                      
             `car_id` int(10) unsigned NOT NULL,                                                                                  
             PRIMARY KEY  (`fav_id`),                                                                                              
             KEY `FK_Favorites` (`user_id`),                                                                                       
             CONSTRAINT `FK_Favorites` FOREIGN KEY (`car_id`) REFERENCES `car` (`car_id`) ON DELETE CASCADE ON UPDATE CASCADE  
) ENGINE=InnoDB;

我有问题,显示所有按汽车年份订购的超过 3 人的汽车。非常感谢,对不起我的英语不好

4

2 回答 2

1
SELECT 
    c.car_id
    ,c.car_year
    ,COUNT(p.peo_id)
FROM car c
INNER JOIN people p on c.card_id = p.car_id
GROUP BY    c.car_id ,c.car_year
HAVING  COUNT(p.peo_id) > 3
ORDER BY c.car_year
于 2013-06-25T16:20:57.327 回答
0

首先,找到三人以上的汽车数量

select car_id
from people
group by car_id
having count(*) > 3

现在,获取汽车详细信息

select * from car
where car_id in
(
 select car_id
    from people
    group by car_id
    having count(*) > 3
)
order by car_year
于 2013-06-25T16:14:27.103 回答