SQL JOIN查询提供不准确的返回问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL JOIN查询提供不准确的返回问题相关的知识,希望对你有一定的参考价值。

我已经花了几个小时在网站上搜索这个特定的问题,虽然很多都接近不完全解决它。 如果存在的话,请把它链接起来,我很乐意把这个问题拿下来。

我有两个表 wb_stateswb_cities 你可以从钥匙上看到。

composite state_code+country_code & city_id 是他们各自表的独特之处。

enter image description here

目的:运行一个JOIN查询,以拉入 state_name 从 wb_states 表中获取

我总是得到 sql_mode=only_full_group_by。 ERROR,除非我使用以下方法

SELECT ANY_VALUE(c.city_id) as id, 
       ANY_VALUE(c.city_ascii) as ci,
       ANY_VALUE(s.state_name_ascii) as ps, 
       ANY_VALUE(c.country_code) as co
       FROM wb_cities AS c
       JOIN wb_states AS s ON
       s.state_code=c.state_code
       WHERE c.city_ascii like  'aa%' GROUP BY id ORDER BY co,ps,ci ASC

当然,查询是可以的,但是当我们采用ANY_VALUE的时候,就会失败。

独特的id 2959927 - 市 Aalen 属于一个省 Baden-Wurttemberg. 你是如何构造这个查询的? 谢谢你。

enter image description here

CREATE TABLE `wb_cities` (
  `city_id` int(11) NOT NULL,
  `city_name` varchar(65) DEFAULT NULL,
  `city_ascii` varchar(65) DEFAULT NULL,
  `state_code` varchar(55) DEFAULT NULL,
  `state_name_ascii` varchar(45) DEFAULT NULL,
  `country_code` varchar(45) DEFAULT NULL,
  `country_full` varchar(55) DEFAULT NULL,
  `lat` varchar(45) DEFAULT NULL,
  `lon` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`city_id`)
)

CREATE TABLE `wb_states` (
  `id` int(11) DEFAULT NULL,
  `state_code` varchar(10) NOT NULL,
  `state_name` varchar(45) DEFAULT NULL,
  `state_name_ascii` varchar(45) DEFAULT NULL,
  `country_code` varchar(45) NOT NULL,
  PRIMARY KEY (`state_code`,`country_code`)
)

INSERT INTO `wb_cities` VALUES (14256,'Āzādshahr','Azadshahr','09','Hamadan','IR','Iran','34.79049','48.57011'),(18918,'Protaras','Protaras','01','Famagusta','CY','Cyprus','35.0125','34.05833'),(23814,'Kahrīz','Kahriz','13','Kermanshah','IR','Iran','34.3838','47.0553'),(24851,'Nūrābād','Nurabad','23','Lorestan','IR','Iran','34.0734','47.9725'),(32723,'Īstgāh-e Rāh Āhan-e Garmsār','Istgah-e Rah Ahan-e Garmsar','25','Semnan','IR','Iran','35.23455','52.30942'),(32767,'Qarchak','Qarchak','26','Tehran','IR','Iran','35.42867','51.57544'),(32909,'Shahre Jadide Andisheh','Shahre Jadide Andisheh','26','Tehran','IR','Iran','35.6803','51.0193'),(41210,'Khorramdarreh','Khorramdarreh','36','Zanjan Province','IR','Iran','36.20898','49.19152'),(50672,'Wanlaweyn','Wanlaweyn','14','Shabeellaha Hoose','SO','Somalia','2.6185','44.8938'),(52867,'Qoryooley','Qoryooley','14','Shabeellaha Hoose','SO','Somalia','1.78784','44.52999');

INSERT INTO `wb_states` VALUES (29,'00','Armenia','Armenia','AM'),(11,'00','Aruba','Aruba','AW'),(15,'00','Bosnia and Herzegovina','Bosnia and Herzegovina','BA'),(31,'00','Botswana','Botswana','BW'),(7,'00','Belarus','Belarus','BY'),(23,'00','Cook Islands','Cook Islands','CK'),(39,'00','Christmas Island','Christmas Island','CX'),(4,'00','Czech Republic','Czech Republic','CZ'),(17,'00','Western Sahara','Western Sahara','EH'),(10,'00','Falkland Islands (Islas Malvinas)','Falkland Islands (Islas Malvinas)','FK'),(26,'00','Abkhazia','Abkhazia','GE'),(27,'00','Ghana','Ghana','GH'),(37,'00','Gibraltar','Gibraltar','GI'),(3,'00','Equatorial Guinea','Equatorial Guinea','GQ'),(16,'00','South Georgia and The South Sandwich Islands','South Georgia and The South Sandwich Islands','GS'),(28,'00','Hong Kong','Hong Kong','HK'),(6,'00','Indonesia','Indonesia','ID'),(8,'00','Ireland','Ireland','IE'),(32,'00','Kyrgyzstan','Kyrgyzstan','KG'),(36,'00','Comoros','Comoros','KM'),(34,'00','Muḩāfaz̧atalWafrah','MuhafazatalWafrah','KW'),(19,'00','Zhezqazghan Oblysy','Zhezqazghan Oblysy','KZ'),(21,'00','Lithuania','Lithuania','LT'),(40,'00','Monaco','Monaco','MC'),(13,'00','Montenegro','Montenegro','ME'),(9,'00','Mali','Mali','ML'),(5,'00','Macedonia','Macedonia','MO'),(35,'00','Mauritius','Mauritius','MU'),(25,'00','Malaysia','Malaysia','MY'),(41,'00','Norfolk Island','Norfolk Island','NF'),(30,'00','Bāgmatī Zone','Bagmati Zone','NP'),(2,'00','Niue','Niue','NU'),(14,'00','Panama','Panama','PA'),(22,'00','Philippines','Philippines','PH'),(42,'00','Palestine','Palestine','PS'),(43,'00','Serbia','Serbia','RS'),(24,'00','Swaziland','Swaziland','SZ'),(20,'00','Turks and Caicos Islands','Turks and Caicos Islands','TC'),(33,'00','Turkmenistan','Turkmenistan','TM'),(38,'00','Turkey','Turkey','TR'),(12,'00','British Virgin Islands','British Virgin Islands','VG'),(18,'00','Zimbabwe','Zimbabwe','ZW'),(164,'01','Abū Z̧aby','Abu Zaby','AE'),(90,'01','Badakhshan','Badakhshan','AF'),(112,'01','Barbuda','Barbuda','AG'),(118,'01','Aragatsotn','Aragatsotn','AM'),(124,'01','Benguela','Benguela','AO'),(45,'01','Buenos Aires','Buenos Aires','AR'),(146,'01','Burgenland','Burgenland','AT');
答案

你加入 wb_citieswb_states 仅在 state_code 但你也应该使用 country_code因为 state_code 无独有偶 wb_states. 另外,我也没有看到任何聚合的理由。你只需要一个表的连接。

SELECT c.city_id id, 
       c.city_ascii ci,
       s.state_name_ascii ps, 
       c.country_code co
FROM wb_cities AS c JOIN wb_states AS s 
ON s.state_code=c.state_code AND s.country_code = c.country_code
WHERE c.city_ascii like  'aa%' 
ORDER BY co, ps, ci 
另一答案

你的数据模型被破坏了. 这里有两个定义 state_code:

`state_code` varchar(55) 
`state_code` varchar(10)

但这是未申报的外国钥匙关系的一部分。

我认为这是根本问题。

PRIMARY KEY (`state_code`, `country_code`)

据推测, ID 是独一无二的,所以你应该有。

CREATE TABLE `wb_states` (
  `state_id` int(11) DEFAULT PRIMARY KEY,
  `state_code` varchar(10) NOT NULL,
  `state_name` varchar(45) DEFAULT NULL,
  `state_name_ascii` varchar(45) DEFAULT NULL,
  `country_code` varchar(45) NOT NULL,
  `country_full` varchar(55) DEFAULT NULL,
  UNIQUE (`state_code`,`country_code`)
);

那么,你... wb_cities 表可以适当地定义为。

CREATE TABLE `wb_cities` (
  `city_id` int(11) NOT NULL PRIMARY KEY,
  `city_name` varchar(65) DEFAULT NULL,
  `city_ascii` varchar(65) DEFAULT NULL,
  `state_id` int DEFAULT NULL,
  `lat` varchar(45) DEFAULT NULL,
  `lon` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`city_id`),
  FOREIGN KEY (state_id) REFERENCES wb_states(state_id)
);

你可能也应该有一个 "国家 "表 但至少国家可以放在 "州 "表而不是 "城市 "表里。

然后,"国家 "的信息可以用 "国家 "表来查询。state_id.

固定好数据模型后,就可以考虑固定查询了。

以上是关于SQL JOIN查询提供不准确的返回问题的主要内容,如果未能解决你的问题,请参考以下文章

spark关于join后有重复列的问题(org.apache.spark.sql.AnalysisException: Reference '*' is ambiguous)(代码片段

求助sql语句,多个join嵌套

带有 CASE 和 JOIN SUBQUERY 的 Oracle SQL 查询

MS Access 2010 - 使用 RIGHT JOIN 的 SQL 查询 - 返回太多值

SQL多表查询

sql join 与列上的多个条件