我是查询编写的新手,并编写了一个类似
的查询SELECT *
FROM Pages P
INNER JOIN PageTypes PT ON PT.ID = P.pagetypes_id
INNER JOIN Paragraphs Para ON Para.page_id = P.id
WHERE PT.mame = 'News'
AND Para.layout_id = (SELECT ID FROM Layout_Scripts WHERE name = 'intro')
它似乎有用,但我不确定这是编写这样一个查询的好方法还是有其他方法来编写一个优化查询
答案 0 :(得分:-1)
我认为,这是更好的
DECLARE @ID int
SELECT ID into @ID FROM Layout_Scripts WHERE name = 'intro'
SELECT *
FROM Pages P
INNER JOIN PageTypes PT ON PT.ID = P.pagetypes_id
INNER JOIN Paragraphs Para ON Para.page_id = P.id
WHERE PT.mame = 'News'
AND Para.layout_id = @ID
因为如果你在where中写,它会为每一行执行查询 但我认为ID只有1,如果你把它放在变量
中会更好