Hello,

There is an online store, in the basket there is a selection of goods by ID, and it is necessary that in the basket some goods are not more than 2 in number, and thus in 1 basket there can be several products with 1 ID.

But in the sample can not be done:

SELECT * FROM goods WHERE id IN (101842,101842,101838,101840) 

But there are only 3 elements in the sample, how can you form a query so that all 4 elements are in the sample?

  • And what prevents getting the results from the database, duplicate them the necessary number of times? The base probably does not know how much of what kind of goods is ordered. And you are trying to "shove" business logic into it. Yes, you need to check if there is so much product, and it is still possible to reserve (otherwise, in the meantime, another customer will buy it?) - VladD

2 answers 2

If the ID in the basket is the primary key, then the quantity column is required. If - not the key, then enough

 WHERE id IN (101842,101838,101840) 

to display all products with the same ID.

  • id is just the key - Oleg Zagorodni
  • Well, then the first script is a column with a number + a limit on the number greater than 2. As far as I remember, MySQL does not support CHECK constraints, because this restriction will have to be implemented in the application code. - msi

Strange Wishlist, but if you really want to ..

 SELECT * FROM goods WHERE id = (101842,101838,101840) union all SELECT * FROM goods WHERE id = 101842 ....