在过去一年左右的时间里,我大部分时间都在使用noSQL数据库。也就是说,我已经开始了一项适用于SQL数据库和SQL Server Management Studio(SSMS)的新工作。任何可以提高可读性并使查询更简洁的建议都将非常受欢迎。
SELECT DISTINCT
[db1].[id] as "Node ID",
[db2].[name] as "Node Name",
[db3].[name] as "ISP",
[db4].[name] as "City",
CASE
WHEN [db1].[object_type_id] = 17
THEN 'Client'
WHEN [db1].[synthetic_location].[object_type_id] = 5
THEN 'System'
WHEN [db1].[object_type_id] IS NULL
THEN 'System'
END AS Type
FROM
[db1].[synthetic_location]
JOIN
[db2].[machine] ON [db2].[synthetic_location_id] IS NULL
JOIN
[db3].[internet_service_provider] ON [db3].[id] = [db1].[internet_service_provider_id]
JOIN
[db4].[geography_city] ON [db4].[geography_city].[id] = [db1].[synthetic_location].[geography_city_id]
WHERE
[db2].[status_type_id] < 1
AND [db1].[flags] = 6
答案 0 :(得分:1)
您的查询看起来很好,并且完全可读,但有一个例外;
您应该明确说明INNER JOIN
而不是JOIN
,因为它会使意图更清晰。
答案 1 :(得分:0)
为了使其更简洁,请使用别名。
如果名称中只有字母,数字或下划线,或者名称不是某个sql关键字,则不需要括号。 (虽然i.m.h.o.这些括号通常使SQL更具可读性)
select warehouse_id, warehouse_name, SUM(available_quantity) available_quantity
from stock join warehouse s on s.id=warehouse_id
WHERE org_id='152
group by warehouse_id
答案 2 :(得分:0)
您可以使用SSMS Boost或SQL complete等工具来帮助您格式化查询,这些工具有很多其他功能可以节省大量时间