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 

本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/493.html

(0)
上一篇 2022年6月29日 下午3:57
下一篇 2022年6月29日 下午3:57

相关推荐

发表评论

登录后才能评论