从国家,城市,大陆选择计数

时间:2011-12-23 00:07:38

标签: mysql

我有一个有国家,城市和大陆地区的桌子。

当一些人搜索一个家时,如果他输入例如“rom”我想要返回

  • 罗马(30)
  • 罗马尼亚(120)

我该怎么做?

修改 使用此处的搜索表单http://www.holiday-rentals.co.uk/查看我的意思

修改 这是我到目前为止所尝试的

SELECT DISTINCT COUNT(country),COUNT(city),COUNT(continent) FROM homes WHERE country LIKE '%rom%' OR city LIKE '%rom%' OR continent LIKE '%rom%';

提前致谢

1 个答案:

答案 0 :(得分:1)

增强列文的答案:

    SELECT   'city' AS result_type
           , city
           , COUNT(*) AS cnt
    FROM     homes
    WHERE    city LIKE '%rom%'
    GROUP BY city
UNION ALL
    SELECT   'country' AS result_type
           , country
           , COUNT(*) AS cnt
    FROM     homes
    WHERE    country LIKE '%rom%'
    GROUP BY country
UNION ALL
    SELECT   'continent' AS result_type
           , continent
           , COUNT(*) AS cnt
    FROM     homes
    WHERE    continent LIKE '%rom%'
    GROUP BY continent