一.sql语法
二.sql语句的报错问题,代码中显示的都是错误代码
select for world
1.查询课程编号为'0002'的总成绩
[SQL]
select sum(成绩)
from score
where 课程号='0002'
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from score
where 课程号='0002'' at line 2
报错翻译:你有一个错误的sql语法,请检查'from score where 课程号='0002'' 附近的语法。这里暗示问题不出在这一句,而在其它句
分析:sql语句中对中英文符号混用是最常见的报错原因,需要检查的符号有括号,逗号,等于号,分号
2.“墨西哥墨西哥”的首都是“墨西哥城”。
显示所有国家名字,其首都是国家名字加上“ City”。
SELECT name
FROM world
-- WHERE capital = concat(name,'City')错误
-- WHERE capital like concat(name,'City')错误
WHERE capital like concat(name,' City')
错误原因:题目要求首都名字是国家名字 加上 City , 被我理解为’等于‘关系
模糊查询是指 系统允许被搜索信息和提问信息之间存在一定差异,给这个差异打分【0,无穷)。当差异为0时,这种情况是指没有差异。
错误原因: Mexico City,两个名词之间的连接是有空格的,所以‘ City’,就是City前要有空格。
3.找出所有首都和其國家名字,而首都是國家名字的延伸。
你應顯示 Mexico City,因它比其國家名字 Mexico 長。
你不應顯示 Luxembourg,因它的首都和國家名相是相同的。
select name,capital
from world
where capital like concat(name,'_%') and capital != name
4. 发现所有首都和其国家名字,而首都要有国家名字中出现。
select capital,name
from world
-- where capital = '%name%'错误
where capital like concat('%',name,'%')
错误原因:capital = ‘%name%’表示?,没有百度到,猜测它在有‘=’时,就是单纯表示%,而不代表字符串。但是查看sql教程,我是在模糊查询这个知识点了解到%,_,那么这是不是说明它们只能用于模糊查询。
5."Monaco-Ville"是合併國家名字 "Monaco" 和延伸詞"-Ville".
顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
select name,replace(capital,name,'')
from world
where capital like concat('%',name,'%') and capital <> name
知识点:
1.replace(区域,‘被替代内容’,‘替换内容’)2.<>表示不等于,有时也被写作 !=
select for nobel
1. 顯示1980年至1989年(包含首尾)的文學獎(Literature)獲獎者所有細節(年,主題,獲獎者)。
select yr,subject,winner
from nobel
-- where 1980<=yr<=1989 and subject = 'Literature',错误的
where yr between 1980 and 1989 and subject = 'Literature'
错误原因:虽然where查询条件可以使用比较运算符,如<=,但是并不存在上面这种表达,而是用between...and...(包头包尾)
2. 顯示1980年物理學(physics)獲獎者,及1984年化學獎(chemistry)獲得者。
select *
from nobel
where (yr = 1980 and subject = 'physics')
or (yr=1984 and subject='chemistry')
解法总结:a or b,查询到a胡哦b中的某一个之后,会继续查询直到查询到所有内容。也就是把a,b两个条件都查询到
3. 查看1980年獲獎者,但不包括化學獎(Chemistry)和醫學獎(Medicine)。
select winner
from nobel
-- where yr=1980 , not subjuect in ('Chemistry','Medicine')
where yr=1980,suject not in ('Chemistry','Medicine')
错误原因:顺序错误。
4. 查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 Find all details of the prize won by EUGENE O'NEILL
select *
from nobel
-- where winner= 'EUGENE O'NEILL' 错误:sql字符串包含单引号
where winner = 'EUGENE O''NEILL'
改正措施: sql字符串中也有单引号时,把单引号变成两个单引号