通过引用另一个表的列计数(*)从一个表中获取数据

时间:2014-04-12 10:48:53

标签: c# mysql

我有两张桌子" domaininfo""和"事件" .domain信息的列名称为" city" 以同样的方式表名"事件"列名称为" city"。在这种情况下,我想从域名中获取数据,其中事件中有记录> 200.I想要从域名引用到城市的200以上的记录两张桌子。威胁参考城市名称" evets"表格已超过200,已经在domainname.I尝试了这个查询。

Domaininfo的表结构     ID City州的国家

事件的表结构     EventName地址城市描述

城市专栏有" domaininfo"从哪个表中可以获取事件的引用。所以我想要来自" domianinfo"那些在"事件"中发生事件的城市表超过200。

For Example: domain info has city name "New York"
             in that case i want to check whether there are the events more than 200 in the "events" table.If "events" table will have the records more than less than 200 it will give me the record from "domaininfo" table.

select count(*) 
  from wpcommon.domaininfo 
 inner join events by city
HAVING COUNT(evets)>200;

select count(*) 
  from wpcommon.domaininfo
  where wpcommon.evets
 having count(*)>200;

select count(*)
 from wpcommon.domaininfo
where wpcommon.evets.select count(*)>200;

1 个答案:

答案 0 :(得分:0)

让我们构建它,利用SQL中的S(结构化)。

首先,city的值超过200个事件?

SELECT COUNT(*) AS eventcount,
       city
  FROM events
 GROUP BY city
HAVING COUNT(*) > 200

该子查询生成您想要的城市列表。

接下来,让我们将其加入您的活动列表。

SELECT d.something,
       d.somethingelse,
       d.whatever,
       d.whatelse
  FROM domaininfo AS d
  JOIN (
        SELECT COUNT(*) AS eventcount,
               city
          FROM events
         GROUP BY city
        HAVING COUNT(*) > 200
       ) AS big ON d.city = big.city

结构化(嵌套)查询将产生您希望的结果。

但是要注意,城市名称本身在美国并不是唯一的。考虑"堪萨斯城"它存在于堪萨斯州和密苏里州。您可能希望使用城市/州组合或ID。

相关问题