[英]SQL: combining two sql queries
I have two tables:我有两个表:
T_CHAMBRE_CHB (CHB_NUM);
T_PLANNING_PLN (PLN_JOUR ,CHB_NUM, PLN_LIBRE);
I have these values in it:我有这些价值观:
T_CHAMBRE_CHB: T_CHAMBRE_CHB:
1
2
3
4
T_PLANNIG_CHB: T_PLANNIG_CHB:
1 2000-01-12 1 False
2 2000-01-12 2 False
3 2000-01-13 1 False
4 2000-01-13 2 False
5 2000-01-13 4 True
I would like to get the rooms occpation the 13 janvier 2000 for information if a room is not in the planning (here 3) that's mean it's free, so the result should be:如果房间不在规划中(此处为 3),我想获取 13 janvier 2000 的房间信息,这意味着它是免费的,所以结果应该是:
CHB_NUM PLN_LIBRE
----------- ---------
1 False
2 False
3 True
4 True
I have this query:我有这个查询:
SELECT DISTINCT TCC.CHB_NUM, TPP.PLN_LIBRE
FROM T_CHAMBRE_CHB AS TCC,
T_PLANNING_PLN AS TPP
WHERE TCC.CHB_NUM=TPP.CHB_NUM AND TPP.PLN_JOUR = '2000-01-13'
UNION
SELECT DISTINCT TCC.CHB_NUM, TPP.PLN_LIBRE
FROM T_CHAMBRE_CHB AS TCC,
T_PLANNING_PLN AS TPP
WHERE TCC.CHB_NUM NOT IN (SELECT DISTINCT (TPP2.CHB_NUM) FROM T_PLANNING_PLN AS TPP2);
I get this result:我得到这个结果:
1 1 False
2 2 False
3 3 False
4 3 True
5 4 True
I don't know why I get the third line ( 3---->False )我不知道为什么我得到第三行( 3---->False )
I think I should use an OUTER UNION but SQL Server doesn't like the syntax.我想我应该使用 OUTER UNION 但 SQL Server 不喜欢这种语法。
You have to use left join if I am understanding your question.如果我理解你的问题,你必须使用左连接。
Please use below query may be it helps you请使用以下查询可能对您有帮助
SELECT T_CHAMBRE_CHB.CHB_NUM, ISNULL(T_PLANNIG_CHB.PLN_LIBRE, FALSE)
FROM T_CHAMBRE_CHB
LEFT JOIN T_PLANNIG_CHB ON (T_CHAMBRE_CHB.CHB_NUM = T_PLANNIG_CHB.CHB_NUM)
WHERE T_PLANNIG_CHB.PLN_JOUR = '2000-01-13'
Below query might give the required result :-下面的查询可能会给出所需的结果:-
declare @date as date='2000-01-13'
select distinct TCC.CHB_NUM,COALESCE(TPP.PLN_JOUR,@date) PLN_JOUR,COALESCE(TPP.PLN_LIBRE,'True') PLN_LIBRE
from T_CHAMBRE_CHB TCC
LEFT OUTER JOIN
T_PLANNING_PLN TPP ON TCC.CHB_NUM=TPP.CHB_NUM
WHERE TPP.PLN_JOUR=@date OR TPP.PLN_JOUR IS NULL
Output :-输出 :-
CHB_NUM PLN_JOUR PLN_LIBRE
1 2000-01-13 False
2 2000-01-13 False
3 2000-01-13 True
4 2000-01-13 True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.