是否有清除此SQL查询的选项

时间:2017-03-14 11:13:00

标签: sql select

我想知道是否可以将此查询缩小

SELECT Templates.TempName
      ,TimeSpans.TimeSpan
      ,TemplateSlotTypes.[Type]
      ,TemplateSlots.Potition
      ,TemplateSlots.duration
FROM TimeSpans
INNER JOIN Templates ON  Templates.TimeSpanId = TimeSpans.Id
INNER JOIN TemplateSlots ON  Templates.Id = TemplateSlots.TemplateId 
INNER JOIN TemplateSlotTypes ON  TemplateSlots.[Type] = TemplateSlotTypes.Id

2 个答案:

答案 0 :(得分:1)

您似乎需要此查询的所有部分,因此无法从查询中删除任何内容。 但是,您可以使用表别名来减少文本量:

SELECT t.TempName, tsp.TimeSpan, tst.[Type], ts.Potition, ts.duuration
FROM TimeSpans tsp
INNER JOIN Templates t ON  t.TimeSpanId = tsp.Id
INNER JOIN TemplateSlots ts ON  t.Id = ts.TemplateId 
INNER JOIN TemplateSlotTypes tst ON  ts.[Type] = tst.Id

答案 1 :(得分:0)

表别名将使连接更简洁,但假设字段名称对于所涉及的表是唯一的,则不需要为它们添加表名前缀。

SELECT TempName
      ,TimeSpan
      ,[Type]
      ,Potition
      ,duration
FROM TimeSpans ts
INNER JOIN Templates t ON  t.TimeSpanId = ts.Id
INNER JOIN TemplateSlots s ON t.Id = s.TemplateId 
INNER JOIN TemplateSlotTypes st ON  s.[Type] = st.Id

但是,我建议使用一个好的IDE进行SQL查询开发,它会为你执行连接并删除大量的输入。

从长远来看,手动减少查询中的文本数量是错误的经济。它实际上可以使SQL更难阅读,因此将来修改或调试会更耗时。

相关问题