Home mysql incorrectly outputs Sum MySQL

incorrectly outputs Sum MySQL

Author

Date

Category

Please tell me why in this query displays 2500 and not 5000.

select sum (Price) from Products WHERE ID IN
(Select ID_Product From Order_Products WHERE ID_Order = 1)
Products Table
ID | Price
1 | 2500.
Order_Products Table
ID | ID_Order | ID_Product.
1 | 1 | 1
2 | 1 | 1

Answer 1

Because it is necessary to teach normal joyans. And subqueries are such joyans for the poor.

Here it turns out a request

select sum (Price) from Products Where ID in (1,1)

which is actually syntax sugar for request

select sum (Price) from Products Where id = 1 or id = 1

which returns 1 line

And you need a request that returns 2 lines. And it will make the usual simple joyne

select sum (Price) from Order_Products Op, Products P
Where p.ID = OP.ID_Product and id_order = 1

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions