创建正确的表后,查询在 SQLite 中对我来说运行得很好。这是未转义和格式化的完整查询:
SELECT
"x0"."ID",
"x0"."NAME",
"x0"."COUNTRYCODE",
"x0"."DISTRICT",
"x0"."POPULATION"
FROM
"CITY" "x0"
INNER JOIN
"COUNTRY" "x1"
ON
"x0"."COUNTRYCODE" = "x1"."CODE"
WHERE ("x1"."NAME" = ?)
这是要测试的 DDL 和插入:
CREATE TABLE Country (CODE text, Name Text);
CREATE TABLE CITY (ID int, Name text, countrycode text, district text, population int);
INSERT INTO Country VALUES ('US', 'United States');
INSERT INTO Country VALUES ('AU', 'Australia');
INSERT INTO City VALUES (1, 'Albany', 'US', 'NY', 1000000);
INSERT INTO City VALUES (2, 'Atlanta', 'US', 'GA', 2000000);
INSERT INTO City VALUES (3, 'Washington', 'US', 'DC', 500000);
INSERT INTO City VALUES (4, 'Melborne', 'AU', 'A', 40000);
INSERT INTO City VALUES (5, 'Sydney', 'AU', 'B', 60000);
完整示例(在控制台中运行,因此查询未参数化,但?
SQLite 接受参数化)。
C:\Windows\System32>sqlite3
SQLite version 3.7.6.3
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE Country (CODE text, Name Text);
sqlite> CREATE TABLE CITY (ID int, Name text, countrycode text, district text, population int);
sqlite> INSERT INTO Country VALUES ('US', 'United States');
sqlite> INSERT INTO Country VALUES ('AU', 'Australia');
sqlite> INSERT INTO City VALUES (1, 'Albany', 'US', 'NY', 1000000);
sqlite> INSERT INTO City VALUES (2, 'Atlanta', 'US', 'GA', 2000000);
sqlite> INSERT INTO City VALUES (3, 'Washington', 'US', 'DC', 500000);
sqlite> INSERT INTO City VALUES (4, 'Melborne', 'AU', 'A', 40000);
sqlite> INSERT INTO City VALUES (5, 'Sydney', 'AU', 'B', 60000);
sqlite> SELECT
...> "x0"."ID",
...> "x0"."NAME",
...> "x0"."COUNTRYCODE",
...> "x0"."DISTRICT",
...> "x0"."POPULATION"
...> FROM
...> "CITY" "x0"
...> INNER JOIN
...> "COUNTRY" "x1"
...> ON
...> "x0"."COUNTRYCODE" = "x1"."CODE"
...> WHERE ("x1"."NAME" = 'United States');
1|Albany|US|NY|1000000
2|Atlanta|US|GA|2000000
3|Washington|US|DC|500000
sqlite>