SQL根据条件获得其他结果

时间:2018-09-18 17:43:20

标签: sql sql-server

我有一个简单的sql表,其中包含4列(dateSlot(varchar),timeSlot(varchar),slotOrder(int)和Taked(布尔)

我的数据看起来像这样:

<LCTimeSlots>
<dateSlot>Wednesday, September, 26th, 2018</dateSlot>
<id>1</id>
<slotOrder>1</slotOrder>
<taken>false</taken>
<timeSlot>4:00pm</timeSlot>
</LCTimeSlots>
<LCTimeSlots>
<dateSlot>Wednesday, September, 26th, 2018</dateSlot>
<id>2</id>
<slotOrder>2</slotOrder>
<taken>false</taken>
<timeSlot>5:00pm</timeSlot>
</LCTimeSlots>
<LCTimeSlots>
<dateSlot>Thursday, September, 27th, 2018</dateSlot>
<id>3</id>
<slotOrder>3</slotOrder>
<taken>false</taken>
<timeSlot>4:00pm</timeSlot>
</LCTimeSlots>
<LCTimeSlots>
<dateSlot>Thursday, September, 27th, 2018</dateSlot>
<id>4</id>
<slotOrder>4</slotOrder>
<taken>false</taken>
<timeSlot>5:00pm</timeSlot>
</LCTimeSlots>

我得到的数据如下:

SELECT id, dateSlot, timeSlot, slotOrder, taken FROM LCTimeSlots

我想做的是写一个条件,该条件仅显示2018年9月26日星期三的记录,而不显示2018年9月27日星期四的记录,直到标记了2018年9月26日星期三的所有记录就像...我想做的是可能的吗?

1 个答案:

答案 0 :(得分:1)

我建议类似这样的内容(尽管对于SQL Server而言可能不是确切的语法)

SELECT id, dateSlot, timeSlot, slotOrder, taken FROM LCTimeSlots WHERE dateSlot = 'Wednesday, September, 26th, 2018' 
UNION SELECT id, dateSlot, timeSlot, slotOrder, taken FROM LCTimeSlots WHERE dateSlot = 'Thursday, September, 27th, 2018' 
AND NOT EXISTS (SELECT 1 FROM FROM LCTimeSlots WHERE dateSlot = 'Wednesday, September, 26th, 2018' AND taken = false);