【发布时间】:2019-07-27 10:47:55
【问题描述】:
我有三个名为 Service 、 Location 和 SeriveLocation 的表。
Service表数据如下
ServiceID Service SortValue
1 Customer call 5
2 Reload 2
3 Internet setting 3
4 E care 7
5 Anti call 4
6 MMS 1
7 settings 6
和SeriveLocation表数据如下
FkLcoationID FkServiceID SortValue
001 1 2
002 1 1
003 1 2
004 1 NULL
005 1 4
001 2 3
002 2 4
003 2 4
004 2 NULL
005 2 1
001 3 4
002 3 2
003 3 3
004 3 NULL
005 3 3
001 4 1
002 4 3
003 4 1
004 4 NULL
005 4 2
现在我需要通过SeriveLocation 表的sortValue 获取其services 和serviceIDs 的特定位置数据。如果SeriveLocation 的表sortValue 是NULL 我需要使用Service 表的SortValue 获得服务订单
我的预期输出是locationId = 001
当LocationId = 004时(根据映射表,位置004没有sortVlaue,所以它的排序顺序需要取自服务表)
我该怎么做?
我试过这个,当 Sortvalue 为 Null 时
select sm.FkBranchId,sm.FkServiceId,s.ServiceName from SeriveLocation sm
INNER JOIN Service s ON s.ServiceID = sm.FkServiceId
where sm.FkLocationId = 004
order by s.SortValue
【问题讨论】:
-
到目前为止你尝试过什么?看起来您只需要一个
JOIN和一个ORDER BY子句。 -
@Larnu 我是新来的先生,你能帮帮我吗
-
查看
JOIN和ORDER BY的语法。最好的学习方法是研究和尝试;JOIN是 SQL 的基础,因此您需要了解它们。如果您失败了,请编辑您的尝试,以便我们解释您出错的地方。
标签: sql sql-server join sql-order-by