SELECT
id,
name,
cid
from
shbooks
where
cid=(
SELECT
cid
FROM
shbooks
where
name= '光荣与梦想'
);
上面这个我完全看得懂,返回<光荣与梦想>的cid,然后用这个值作为条件,匹配整个表的cid;
SELECT
b1.id,
b1.name,
b1.cid
from
shbooks b1,
shbooks b2
where
b1.cid=b2.cid
and
b2.name='光荣与梦想';
但是这个,条件是 b1.cid=b2.cid 的话,会返回整个表,为什么加上个 b2.name='光荣与梦想' 就能返回 和 光荣与梦想 相同cid 的别的书???
答案不在长,也不在语言是不是准确,只要让我能明白怎么个逻辑就行了..谢谢了!
Original
Condition b1.cid = b2.cid (5 results are obtained by filtering out of 9 results) As long as you understand why there are 9 results, there should be no doubt about the whole problem.
Condition b2.name='Glory and Dream'
then you will get
If your first way of writing contains multiple cids, would it be wrong?
You can refer to the following writing method:
As for your second way of writing, do you understand inner join? Do you think the following writing method makes it easier to understand?
The conditions are two, big brother, b2.name='Glory and Dream' and b1.cid = b2.cid
Just think of it as two different tables, this is a self-join
@broken mirror His is the correct solution, first Cartesian set, and then filter. This is the implementation principle. However, generally this kind of statement SQL will be automatically optimized. It doesn't really do a Cartesian product first and then filter.