Commit ed124077 by ligang

'大的国家'

parent c68264f9
#!/usr/bin/env python3
#-*- coding:utf-8-*-
"""
这里有张 World 表
+-----------------+------------+------------+--------------+---------------+
| name | continent | area | population | gdp |+
-----------------+------------+------------+--------------+---------------+|
Afghanistan | Asia | 652230 | 25500100 | 20343000 |
| Albania | Europe | 28748 | 2831741 | 12960000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000 |
| Andorra | Europe | 468 | 78115 | 3712000 |
| Angola | Africa | 1246700 | 20609294 | 100990000 |
+-----------------+------------+------------+--------------+---------------+
如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。
编写一个SQL查询,输出表中所有大国家的名称、人口和面积。例如,根据上表,我们应该输出
+--------------+-------------+--------------+
| name | population | area |
+--------------+-------------+--------------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+--------------+-------------+--------------+
请写出SQL语句
"""
"""
1、首先创建数据库 country
create database country default character set utf8 collate utf8_general_ci;
2、创建数据表:
create table world(
id int(11) not null auto_increment.
name varchar(64) not null,
continent varchar(20) not null,
area int(11) not null,
population int null,
gdp int(11) null,
primary key(id)
)engine = InnoDB default charset =utf8;
3、插入数据
insert into world(name,continent,area,population,gdp) values('Afghanistan','Asia',652230,25500100,20343000),
('Albania','Europe',28748,2831741,12960000),(...)
"""
import pymysql
#pymysql.install_as_MySQLdb()
mydb = pymysql.Connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='country',
charset='utf8'
)
cursor = mydb.cursor()
sql = " select name,population,area from world where area > 300000 or population > 25000000"
cursor.execute(sql)
rs = cursor.fetchmany()
print(rs)
cursor.close()
mydb.close()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment