我有一个表“FRIENDS”,列“id”,“toid”,“fromid”。 我发现这个奇怪的synthax(对我来说)查询:
SELECT GROUP_CONCAT(friends.myfrndids) myfrndids
FROM ( SELECT toid AS myfrndids FROM `friends`
WHERE status = 1 AND fromid = '231'
UNION
SELECT fromid AS myfrndids FROM `friends`
WHERE status = 1 AND toid = '231'
) friends;
对我来说,奇怪的是第一个FROM之后的括号:FROM (SELECT toid AS myfrndids FROM `friends` WHERE status = 1 AND fromid = '231' UNION SELECT fromid AS myfrndids FROM `friends` WHERE status = 1 AND toid = '231') friends
我一直找到:FROM `table`
有人可以解释一下吗?
答案 0 :(得分:3)
它被称为subquery。您可以选择from子查询的结果。简而言之,它看起来像这样:
SELECT `field` FROM (subquery) AS tbl;
子查询只是常规的SQL查询,例如SELECT field from table
答案 1 :(得分:0)
FROM
子句之后的预期部分是一个表!但它并不总是一个命名表。您还可以在flay上构建一个表,然后从中进行选择!子查询的结果是一个表,然后您从中进行选择!
(SELECT toid as myfrndids FROM 'friends' WHERE status =1 and fromid = '231' union SELECT fromid as myfrndids FROM 'friends' WHERE status =1 and toid = '231')
返回一个表,然后friends
之后给出一个别名(临时昵称),以便稍后可以在WHERE子句中引用它。
我希望这会有所帮助