首页 > 代码库 > 查询表语句,以及嵌套

查询表语句,以及嵌套

技术分享

 

CREATE TABLE classes(
class_id INT PRIMARY KEY auto_increment,
class_name VARCHAR(20),
class_address VARCHAR(20)
class_mainTeacher VARCHAR(20)
)

CREATE TABLE student (
stu_id int PRIMARY KEY auto_increment,
stu_name VARCHAR(20) NOT NULL,
stu_age int NOT NULL,
stu_phone VARCHAR(20) UNIQUE,
stu_qq VARCHAR(20) UNIQUE,
stu_address VARCHAR(20) DEFAULT ‘ChengDuSchool‘,
class_id int,
CONSTRAINT FK_student_classId FOREIGN KEY(class_id) REFERENCES classes(class_id)

)


p.f_name,
p.f_dateOfBirth,
p.f_number,
p.f_place,
p.f_club
FROM
t_player as p
WHERE
-- p.f_club = "皇家马德里" AND p.f_place = "前锋"
-- p.f_place = "门将" AND (p.f_dateOfBirth < ‘1980-1-1‘ OR p.f_dateOfBirth > ‘1970-12-31‘)
-- p.f_club in("阿森纳","AC米兰")
p.f_name LIKE "__斯基"
order by
f_dateofBirth asc


UPDATE t_player SET f_club = NULL WHERE t_player.f_club in (‘未知‘,"无")

-- UPDATE t_player SET f_club = "未知" WHERE t_player.f_club = NULL
SELECT
p.f_name,
p.f_club,
p.f_dateOfBirth,
p.f_place
FROM
t_player as p
WHERE
-- p.f_club = ‘皇家马德里‘ AND p.f_place = ‘前锋‘ AND (p.f_dateOfBirth BETWEEN ‘1980-1-1‘ AND ‘1989-12-31‘);
p.f_place NOT IN(‘门将‘) AND (p.f_dateOfBirth BETWEEN ‘1990-1-1‘ AND ‘1999-12-31‘) AND p.f_name LIKE "%尔%"

SELECT COUNT(*), t_player.f_club FROM t_player GROUP BY f_club ORDER BY COUNT(*) DESC

SELECT COUNT(*), t_player.f_club,t_player.f_place FROM t_player GROUP BY f_club,f_place

SELECT fk_teamid,COUNT(*) from t_player WHERE f_place = ‘后卫‘ GROUP BY fk_teamid HAVING COUNT(*) > 8


-- SELECT contryName FROM bbc WHERE population >= 200000000
-- SELECT contryName,bbc.gdp/bbc.population FROM bbc WHERE population >= 200000000
-- SELECT bbc.contryName,ROUND(bbc.population/1000000) from bbc WHERE bbc.region = ‘south Asia‘
-- SELECT bbc.contryName,bbc.population FROM bbc WHERE bbc.contryName IN ("France","Germany","Italy")
-- SELECT bbc.contryName FROM bbc WHERE bbc.contryName LIKE "%United%"
-- SELECT DISTINCT bbc.region FROM bbc
-- SELECT bbc.contryName,bbc.population FROM bbc WHERE bbc.population >= 100000000 ORDER BY bbc.population DESC

-- SELECT SUM(bbc.population) as 世界人口总和 FROM bbc
-- SELECT DISTINCT bbc.contryArea FROM bbc
-- SELECT bbc.contryName,bbc.gdp FROM bbc WHERE bbc.gdp > (SELECT SUM(bbc.gdp) FROM bbc WHERE bbc.region = "Africa") ORDER BY bbc.gdp DESC
-- SELECT bbc.region,COUNT(*) as 国家总数 FROM bbc GROUP BY bbc.region ORDER BY COUNT(*) DESC
-- SELECT bbc.region,SUM(bbc.population) as 地区总人口,SUM(bbc.contryArea) AS 地区总面积 FROM bbc GROUP BY bbc.region ORDER BY SUM(bbc.population) DESC
-- SELECT bbc.region,COUNT(*) as 该地区人口大于1000万的国家总数 FROM bbc WHERE bbc.population >= 10000000 GROUP BY bbc.region ORDER BY COUNT(*) DESC
-- SELECT bbc.region,SUM(bbc.population) AS 人口总数大于一亿 FROM bbc GROUP BY bbc.region HAVING SUM(bbc.population) >= 100000000 ORDER BY SUM(bbc.population) DESC
-- SELECT SUM(bbc.population) as 欧洲人口总数,SUM(bbc.gdp) as 欧洲总gdp FROM bbc WHERE bbc.region = ‘Europe‘
SELECT bbc.region,SUM(bbc.population) as 总人口数,SUM(bbc.contryArea) as 总面积 FROM bbc GROUP BY region HAVING SUM(bbc.contryArea) >= 1000000 ORDER BY SUM(bbc.population) DESC


SELECT t_player.f_name,(SELECT t_team.f_teamCoach FROM t_team WHERE t_player.fk_teamid = t_team.pk_teamid) FROM t_player WHERE t_player.f_club = "尤文图斯"

SELECT t_player.f_name,(SELECT t_team.f_teamCoach FROM t_team WHERE t_team.pk_teamid = t_player.fk_teamid) as coach,(SELECT t_group.f_groupAddress FROM t_group WHERE t_group.pk_groupid = (SELECT pk_teamid FROM t_team WHERE t_player.fk_teamid = t_team.pk_teamid)) FROM t_player WHERE t_player.f_club = "尤文图斯"

 

查询表语句,以及嵌套