不使用显式的 JOIN 运算符,即可计算学校安全得分为 1 的社区区域的人均收入。
SELECT per_capita_income,community_area_name
FROM CENSUS_DATA
where community_area_name =
(SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )
响应为"per_capita_income"community_area_name"没有价值???感谢大家在下面的所有回复,但结果没有改变,没有价值,但是我尝试了:
SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS
where safety_score='1'
并得到的结果为"华盛顿公园",并做了 seqond 查询
SELECT per_capita_income,community_area_name FROM CENSUS_DATA
where community_area_name = 'WASHINGTON PARK'
我又有了; per_capita_income community_area_name
我已经检查了下面的代码以确认并获取所有per_capita_income值,包括华盛顿公园
SELECT per_capita_income,community_area_name FROM CENSUS_DATA
所以,最后我无法获得capita_income值。数据在以下链接中
CENSUS_DATA:
https://ibm.box.com/shared/static/05c3415cbfbtfnr2fx4atenb2sd361ze.csv
CHICAGO_PUBLIC_SCHOOLS
https://ibm.box.com/shared/static/f9gjvj1gjmxxzycdhplzt01qtz0s7ew7.csv
CHICAGO_CRIME_DATA:
https://ibm.box.com/shared/static/svflyugsr9zbqy5bmowgswqemfpm1x7f.csv
试试这个:
SELECT c.per_capita_income, c.community_area_name
FROM CENSUS_DATA c, CHICAGO_PUBLIC_SCHOOLS s
WHERE s.COMMUNITY_AREA_NUMBER = c.COMMUNITY_AREA_NUMBER AND s.SAFETY_SCORE = '1' ;
UPDATE CENSUS_DATA SET COMMUNITY_AREA_NAME = UPPER(COMMUNITY_AREA_NAME)
select CD.PER_CAPITA_INCOME
from CENSUS_DATA CD,CHICAGO_PUBLIC_SCHOOLS CPS
where CD.COMMUNITY_AREA_NAME = CPS.COMMUNITY_AREA_NAME
and CPS.SAFETY_SCORE = 1
CENSUS_DATA COMMUNITY_AREA_NAME列的值为小写,对于CHICAGO_PUBLIC_SCHOOLS COMMUNITY_AREA_NAME列,它们为大写,因此最好使用 UPDATE 操作更新特定列并执行查询操作以获取所需的答案
看起来您正在寻找如下所示的隐式联接:
SELECT cd.per_capita_income,cd.community_area_name
FROM CENSUS_DATA as cd, CHICAGO_PUBLIC_SCHOOLS as cps
where cd.community_area_name = cps.community_area_name
AND cps.safety_score='1'
正如@lau所评论的那样,您看到的问题很可能来自您的数据。
但是,让我也指出,这种情况存在一个潜在的问题:
WHERE community_area_name =
(SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )
如果CHICAGO_PUBLIC_SCHOOLS
中存在多个安全分数等于 1 的记录,则相等条件将无法正常工作(在大多数 RDBMS 中,这会导致运行时错误)。
此条件应重写为IN
条件:
SELECT per_capita_income,community_area_name
FROM CENSUS_DATA
WHERE community_area_name IN
(SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )
或者作为具有EXIST
条件的相关子查询:
SELECT c.per_capita_income, c.community_area_name
FROM CENSUS_DATA c
WHERE EXISTS (
SELECT 1
FROM CHICAGO_PUBLIC_SCHOOLS s
WHERE s.community_area_name = c.community_area_name AND s.safety_score = '1'
)
我已经查看了数据,正如大家所期望的那样,我可以确认您的数据有问题。
COMMUNITY_AREA_NAME
在CHICAGO_PUBLIC_SCHOOL中为大写这在CENSUS_DATA是恰当的。
始终不同于大写字符串,并且它们都不同于正确大小写的字符串。
正确的解决方案可能是检查数据库设计并将外键放在某个地方。我们没有足够的细节来帮助这样的事情。
例如,不太正确的将所有内容设置为正确,然后重新运行更正后的SELECT
。
UPDATE CENSUS_DATA SET community_area_name = INITCAP(community_area_name);
UPDATE CHICAGO_PUBLIC_SCHOOLS SET community_area_name = INITCAP(community_area_name);
SELECT per_capita_income,community_area_name
FROM CENSUS_DATA
where community_area_name IN
(SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )
SELECT per_capita_income FROM CENSUS_DATA WHERE upper(community_area_name) IN
(SELECT "Community_Area_Name" FROM CHICAGO_PUBLIC_SCHOOLS WHERE "Safety_Score" = 1 )
你的答案什么时候不应该包含任何连接,你必须使用叉积,例如,如果你有 2 个表,你必须使用select * from table1,table2 where condition
在您的情况下,答案应该是
select PER_CAPITA_INCOME from school S,cencus_data C where C.COMMUNITY_AREA_NAME = S.COMMUNITY_AREA_NAME and S.SAFETY_SCORE=1
如果它不起作用,则更改这两个表之间的任何其他公共列
我希望它有所帮助
选项 1:
select PER_CAPITA_INCOME from CENSUS_DATA
where UPPER(COMMUNITY_AREA_NAME) = (
select COMMUNITY_AREA_NAME from CHICAGO_PUBLIC_SCHOOLS where SAFETY_SCORE = 1
);
选项 2:
select PER_CAPITA_INCOME from CENSUS_DATA
where UPPER(COMMUNITY_AREA_NAME) in (
select COMMUNITY_AREA_NAME from CHICAGO_PUBLIC_SCHOOLS where SAFETY_SCORE = 1
)
两者都应该正常工作。
如果你正在Coursera上学习"数据科学的数据库和SQL"课程。问题出在数据集上。如果您查看人口普查数据csv,社区名称为小写,而在学校中csv为大写。所以这应该可以解决问题
%sql 选择COMMUNITY_AREA_NAME,PER_CAPITA_INCOME \从 CENSUS_DATA \其中 UCASE(COMMUNITY_AREA_NAME) = (从CHICAGO_PUBLIC_SCHOOLS中选择"Community_Area_Name",其中"Safety_Score" = 1)
%sql 选择community_area_name,从census_data per_capita_income 其中 community_area_name='华盛顿公园';
我有用。
%%sql
select CPS.COMMUNITY_AREA_NAME, CD.PER_CAPITA_INCOME, CPS.SAFETY_SCORE
from CENSUS_DATA CD, CHICAGO_PUBLIC_SCHOOLS CPS
where CD.COMMUNITY_AREA_NUMBER = CPS.COMMUNITY_AREA_NUMBER
and SAFETY_SCORE = 1;
问题是community_area_name芝加哥学校是大写的,而在人口普查数据中是小写的所以这段代码对我有用,,
%sql 从community_area_number所在的CENUS_DATA中选择 per_capita_income,community_area_number,community_area_name(从"学校"中选择community_area_number,其中 safety_score=1)
%%sql select per_capita_income from CENSUS
where community_area_number = (select community_area_number from SCHOOLS where safety_score = 1)
- 从
'1'
周围取下'