Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql select语句中的多个表可获取单个列_Sql_Mysql Workbench - Fatal编程技术网

Sql select语句中的多个表可获取单个列

Sql select语句中的多个表可获取单个列,sql,mysql-workbench,Sql,Mysql Workbench,这是一个学术问题。我正在使用sql工作台 在sql中,当我们编写 select city.name from city; 执行速度更快,结果显示所有城市名称 但是,如果我写 select city.name from city, country; 执行时间要长得多,结果一遍又一遍地显示一个城市名称 为什么“from”部分中的额外表名会破坏结果?它不应该被完全忽略吗?因为没有连接,基本上是因为缺少WHERE子句而得到a。因为没有连接,基本上是因为缺少WHERE子句而得到a。结果是笛卡尔积。

这是一个学术问题。我正在使用sql工作台

在sql中,当我们编写

select city.name from city;  
执行速度更快,结果显示所有城市名称

但是,如果我写

select city.name from city, country;
执行时间要长得多,结果一遍又一遍地显示一个城市名称


为什么“from”部分中的额外表名会破坏结果?它不应该被完全忽略吗?

因为没有连接,基本上是因为缺少WHERE子句而得到a。

因为没有连接,基本上是因为缺少WHERE子句而得到a。

结果是笛卡尔积。可能查询并不是一次又一次地返回一个相同的名称,它只是为country表中的每行返回一个城市。

结果是笛卡尔乘积。可能查询并不是一次又一次地返回一个相同的名称,它只是为country表中的每一行返回一个城市。

如果需要显示从另一个表(以某种方式)连接到第一个表的数据,请尝试以下操作:

select city.name, country.name
from city, country
where city.country_id = country.country_id
select city.name, country.name
from city
join country on (city.country_id = country.country_id)
或者你可以这样写:

select city.name, country.name
from city, country
where city.country_id = country.country_id
select city.name, country.name
from city
join country on (city.country_id = country.country_id)

如果这很难理解,我会尝试在google上查找并阅读有关“sql联接”的信息:)

如果您需要显示从另一个表(以某种方式)连接到第一个表的数据,请尝试以下操作:

select city.name, country.name
from city, country
where city.country_id = country.country_id
select city.name, country.name
from city
join country on (city.country_id = country.country_id)
或者你可以这样写:

select city.name, country.name
from city, country
where city.country_id = country.country_id
select city.name, country.name
from city
join country on (city.country_id = country.country_id)

如果这很难理解,我会尝试在google上查找并阅读有关“sql连接”的信息:)

阅读有关连接的信息。(您正在执行隐式交叉连接…)或者在本例中,使用WHERE子句使用外键连接两个表。在加入之前的几十年里,就是这样做的。永远不要使用这种语法。它是一种传统,在ANSI 92标准中被取代(大约四分之一个世纪前)
table1,table2
table1交叉连接table2
阅读有关连接的内容相同。(您正在执行隐式交叉连接…)或者在本例中,使用WHERE子句使用外键连接两个表。在加入之前的几十年里,就是这样做的。永远不要使用这种语法。它是一种传统,在ANSI 92标准中被取代(大约四分之一个世纪前)<代码>表1,表2与
表1交叉连接表2