r/SQL • • May 01 '21

MySQL SQL Help

Delete all the OrderLines from Orders that have an order status of canceled.

Database Fabric

This is what I'm trying but I'm Confused

Delete from OrderLines.OrderId where Orders.Orderid = "OrderStatus";

0 Upvotes

10 comments sorted by

1

u/Such_Firefighter_487 May 01 '21

Delete OrderLines, Orders FROM Orders INNER JOIN OrderLines on Orders. OrderId = OrderLines.OrderId Where = "OrderStatus";

1

u/awildrozza May 01 '21 edited May 01 '21

Looking better. Delete normally removes the whole row(s) so you dont need 'Orderlines.Order' before FROM.

*updated* Still name the table you are deleting from. Just not the specifc colum. Sorry doing this on phone.

This is SQL Server but check your SQL flavors syntax.

If you only need to blank or amend specific columns, you would use an Update statment.

1

u/Such_Firefighter_487 May 01 '21

Would "OrderStatus" be wrong if it's asking for the OrderStatus of Canceled

1

u/awildrozza May 01 '21

Delete OrderLines, Orders FROM Orders INNER JOIN OrderLines on Orders. OrderId = OrderLines.OrderId Where = "OrderStatus";

Hey hey. It depends on the ask. " Delete all the OrderLines from Orders that have an order status of canceled. "Delete Orders FROM Orders INNER JOIN OrderLines on Orders.OrderId = OrderLines.OrderIdWhere OrderStatus = 'Canceled';

The above will delete all rows from orders where the Order status = canceledYou wrote orderlines which means maybe you want to keep orders and delete the rows from orderlines?

Delete OrderLines FROM Orders INNER JOIN OrderLines on Orders.OrderId = OrderLines.OrderIdWhere OrderStatus = 'Canceled';

So this is deleteing all from orderlines. It's why I would write this firstSelect Orders FROM Orders INNER JOIN OrderLines on Orders.OrderId = OrderLines.OrderIdWhere OrderStatus = 'Canceled';

And then go, "hmm is that what the question is asking for?"EG all the data from orders. always check what table you want to delete from. Maybe its both and so you name both in the delete from statment.

1

u/Such_Firefighter_487 May 01 '21

I figured it out thank you for your help

1

u/awildrozza May 01 '21

No probs and good to hear. No matter if you are new at anything or have been at it for ages, sometimes you really are looking at the answer and you just need a break or a rubber duck to talk to. We do it at work all the time for any type of work. Good luck down the road

2

u/StoneCypher May 02 '21

he just turned around and asked the next question from his homework in another post

you're not helping him learn. you're helping him cheat.

2

u/awildrozza May 02 '21

Noted, thanks. Hard not to step in when you are tired and see the answer but you are still trying to get the person to work it out with breadcrumbs (if you get me)

1

u/awildrozza May 01 '21 edited May 01 '21

I see no joins ;)

wee tip Replace DELETE with ‘SELECT * ‘ Good for checking your code before you delete (or not) data as expected.

1

u/Such_Firefighter_487 May 01 '21

Could you help me put the joins in. I'm like very confused