我有两个表-订单表和子订单表
订单表
Order_Id |Customer |Status
--------------------------
101 Abc 1
102 xyz 1
子订单表
Sub_Order_Id | Order_Id | Sub_order_status
---------------------------------------
10101 101 2
10102 101 2
10103 101 1
10201 102 1
如果所有子订单ID的状态均为= 2,我需要更新订单表的状态= 2
如果所有子订单状态均为2,则获取订单ID 101
答案 0 :(得分:0)
您可以通过使用相关的子查询来尝试
//This asks the user about how many numbers they are adding
int equations = Convert.ToInt32(Console.ReadKey());
if (equations == '1')
{
Console.WriteLine("That was an incorrect input, please close the application and try again.");
Console.ReadKey();
}
//Start of code for adding 2 numbers
if (equations == '2')
{
Console.WriteLine("What is the first number you would like to add?");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the second number you want to add?");
int number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Press enter to calculate");
Console.WriteLine(number + number2);
}
else
{
Console.WriteLine("That was an incorrect input, please close the application and try again.");
Console.ReadKey();
}
//End of code for adding 2 numbers
//Start of code for adding 3 numbers
if (equations == '3')
{
Console.WriteLine("What is the first number you would like to add?");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the second number you want to add?");
int number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the third number you would like to add?");
int number3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Press enter to calculate your equation");
Console.WriteLine(number + number2 + number3);
}
else
{
Console.WriteLine("That was an incorrect input, please close the application and try again.");
Console.ReadKey();
}
//End of code for adding 3 numbers
//Start of code for adding 4 numbers
if (equations == '4')
{
Console.WriteLine("What is the first number you would like to add?");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the second number you want to add?");
int number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the third number you would like to add?");
int number3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the fourth number you would like to add?");
int number4 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Press enter to calculate your equation");
Console.WriteLine(number + number2 + number3 + number4);
}
else
{
Console.WriteLine("That was an incorrect input, please close the application and try again.");
Console.ReadKey();
}
//End of code for adding 4 number
// Start of code for adding 5 numbers
if (equations == '5')
{
Console.WriteLine("What is the first number you would like to add?");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the second number you want to add?");
int number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the third number you would like to add?");
int number3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the fourth number you would like to add?");
int number4 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the fifth number you would like to add?");
int number5 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Press enter to calculate your equation");
Console.WriteLine(number + number2 + number3 + number4 + number5);
}
else
{
Console.WriteLine("That was an incorrect input, please close the application and try again.");
Console.ReadKey();
}
//End of code for adding 5 numbers
答案 1 :(得分:0)
您可以使用NOT EXISTS
:
update o
set status = 2
from order o
where not exists (
select 1 from suborder
where order_id = o.order_id and sub_order_status <> 2
)
此语法将适用于SQL Server。
请参见demo。
如果您只需要选择订单:
select o.*
from [order] o
where not exists (
select 1 from suborder
where order_id = o.order_id and sub_order_status <> 2
)
答案 2 :(得分:0)
以下查询应执行您想要的操作:
UPDATE o
SET o.[Status] = 2
FROM
[order] o
WHERE o.Order_Id IN (
SELECT Order_Id
FROM [sub_order]
GROUP BY Order_Id
HAVING COUNT(*) = SUM(CASE WHEN Sub_order_status = 2 THEN 1 ELSE 0 END) )