0

我的 SQL 很差,当我在下面得到一个 SQL 查询时,我对其中的aand感到非常困惑b。他们在查询中的作用是什么,它的功能或结果是什么?

SELECT *,(SELECT COUNT(id) FROM abc_menu b WHERE b.parentId=a.id ) FROM abc_menu a ORDER BY ordering ASC
4

1 回答 1

1

a and b are aliases -- in SQL, these are declared immediately after the table-name (in the FROM or JOIN clause) or after a column/ or select-expression.

SELECT *, (
    SELECT COUNT(id) FROM abc_menu b WHERE b.parentId=a.id) 
FROM abc_menu a 
ORDER BY ordering ASC

Here you can see the "outermost" access to the table is aliased (named within this statement) as a.

This distinguishes it from a subquery to count it's children, which references the same table separately aliased as b.

The rows which b refers to are logically independent from the rows that a refers to, and connected only as logically specified -- here, by the subquery WHERE clause b.parentId=a.id.

于 2013-10-12T04:27:02.247 回答