2013-06-25 46 views
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人以上的車的年訂購的所有汽車。 非常感謝你,對不起我的英語不好

+0

什麼做你需要做2個或更多的值?你需要選擇嗎? –

+0

我需要選擇所有超過3人的汽車 –

回答

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 
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 
相關問題