例如,我如何指代儿子这个名字。当我只有母亲的名字时,使用子选择
id-Name - mother
1 -son - 2
2 -mommy - null
这是使用子选择。
SELECT t.*,
(SELECT Name FROM TABLE_NAME WHERE mother = t.id) as sons_name
FROM TABLE_NAME as t
WHERE t.name = 'Mommy'
但是你也可以使用连接来做到这一点:
SELECT t1.*, t2.name as sons_name
FROM TABLE_NAME as t1
LEFT JOIN TABLE_NAME as t2 ON t2.mother = t1.id
WHERE t1.name = 'Mommy'