mysql - 根据国家和地区代码选择地区名称

标签 mysql sql

我有一个名为 profile 的表,其中有一列 region_codecountry_code 以及另一个名为 regions 的表有列 country_coderegion_nameregion_code。我想根据表 profiles< 中的 region_codecountry_code 从表 regions 中选择 region_name/。我认为下面的代码可以工作,但它总是返回一个空集,我无法弄清楚我做错了什么。非常感谢任何帮助。谢谢。

SELECT region_name 
FROM regions 
WHERE country_code IN(
                       SELECT country_code 
                       FROM profiles 
                       WHERE account_name = 'me'
                      )
AND region_code IN(
                       SELECT region_code
                       FROM profiles 
                       WHERE account_name = 'me'
                      )

最佳答案

听起来你想加入?

SELECT r.region_name 
FROM regions r
JOIN profiles p ON r.country_code = p.country_code 
               AND r.region_code  = p.region_code
WHERE p.account_name = 'me'

这将列出映射到特定用户区域代码和国家代码的区域的区域名称。

关于mysql - 根据国家和地区代码选择地区名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028575/

相关文章:

mysql - 选择 3 个随机单词(记录)- 每个字母计数一个

sql - 在类似语句上加入 SQL Server 表

SQL Server 插入语句成功运行,但即使创建了行,值也不会添加到表中

c# - sql中根据ID分别获取Rows

sql - 使用 Django ORM 执行复杂的自引用查询

mysql - 如何通过查询连接组中最近 10 个调用?

php - 在数据库中保存 file_get_contents

mysql - 如何在MySQL中插入语句后获取id

php - SELECT 中带有 CASE 的 SQL 查询不起作用

javascript - addStudent.php 中的 Idnumber 没有重复条目