php – MySQL如何获得表中所有所需字段的正确计数
发布时间:2020-12-13 13:07:20 所属栏目:PHP教程 来源:网络整理
导读:假设我有一张桌子: ID,City1,City2,City3,Country,….(不重要) 该应用程序询问人们他们希望住在哪里,让我们说法国. 因此,必须添加至少一个城市,但您可以添加3个最大城市. 例如,我们在表数据中有: ID City1 City2 City3 Country UserID--------------------
假设我有一张桌子:
ID,City1,City2,City3,Country,….(不重要) 该应用程序询问人们他们希望住在哪里,让我们说法国. 例如,我们在表数据中有: ID City1 City2 City3 Country UserID -------------------------------------------------- 1 Paris / / France 1 2 Paris Nice / France 2 3 Paris Nice / France 3 4 Nice Paris Lyon France 4 5 Lyon Paris Nice France 5 6 Cannes Nice Paris France 6 7 Paris Cannes Lyon France 7 -------------------------------------------------- 现在,当有人点击法国时,我会在页面上显示所有用户. 所以,如果我写: select City1 as city,count(1) as num from table_c where Country = "France" group by City1; 我得到巴黎(4),但我需要得到巴黎(7),因为我还要显示City2和City3,我不知道如何编写这样的SQL语句. 我尝试了很多SQL语句,但后来我得到了几次Paris(n)显示,haw可以做到这一点.如果可以的话?
你可以尝试这个SQL,让我知道这是否有效
select city,count(1) as num from ( select City1 as city from table_c where Country = "France" and city1 is not null UNION ALL select City2 as city from table_c where Country = "France" and city2 is not null UNION ALL select City3 as city from table_c where Country = "France" and city3 is not null ) tbl group by city (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |