如何删除MySQL查询结果中的一列?

时间:2010-07-08 10:40:45

标签: mysql sql

示例:

SELECT `film_id`,COUNT(film_id) AS COUNT FROM films_genres AS FilmsGenre   
WHERE genre_id In (4)  
GROUP BY film_id,COUNT 
HAVING COUNT = 1 

返回:

film_id |  COUNT
7            1   
6            1

但我希望它能够回归:

film_id
  7
  6

如何只返回1个colomn?

3 个答案:

答案 0 :(得分:12)

要做到这一点,只需移动“COUNT(film_id)”即可。您的HAVING条款将为您完成工作。

SELECT `film_id` FROM films_genres AS FilmsGenre
WHERE genre_id In (4)
GROUP BY anime_id,film_id
HAVING COUNT(film_id) = 1

答案 1 :(得分:1)

这不是作为CakePHP问题,尽管它被标记为。

然而,在CakePHP中:

$this->FilmGenre->find('list',array('fields'=>array('film_id','film_id','anime_id')));

答案 2 :(得分:1)

或使用派生表

SELECT film_id from
(
SELECT `film_id`,COUNT(film_id) AS COUNT FROM films_genres AS FilmsGenre    
WHERE genre_id In (4)   
GROUP BY anime_id,COUNT  
HAVING COUNT = 1  
) as t