开发者

why 9 rows are fetch from this query?

开发者 https://www.devze.com 2023-02-04 00:40 出处:网络
Given 2 tables T1 and T2. T1T2 A1 B2 C3 You make a query SELECT * FROM T1, T2. What is the no: of开发者_运维百科 rows that are fetched from this query?
Given 2 tables T1 and T2.
T1  T2 
A   1 
B   2
C   3
You make a query SELECT * FROM T1, T2.
What is the no: of开发者_运维百科 rows that are fetched from this query? 

Answer is 9


This query results in cartesian product because no other conditions are provided. Every row from first table is matched with every row from second table.

The result is

A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3


Because each record from the first table is returned along with each record of the second table and the result is not filtered.

The exact output will be:

T1  T2 
A   1 
A   2
A   3 
B   1
B   2
B   3
C   1
C   2
C   3

(order may vary)


It is a cartesian product: select all rows from one table (3) and all rows from another table (3) and combine them, so 3*3=9.


That's what you asked it to do. You got all the rows from T1 and all the rows from T2. They don't just get added together -- that won't work if the columns are different, for example, though you can do this with UNION -- they get merged in what's known as a "cartesian product". You essentially get all combinations of rows from both tables. And 3*3 = 9.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号