Good day! I decided to make a random withdrawal of goods from the database for an online store, output to produce in the item card. But I'm not sure that this request is safe and correct:

"SELECT * FROM products WHERE id ORDER BY RAND() LIMIT 5 " 

Can this query print two identical items on one item card? Maybe something tell me better. I also saw what they are doing: They also buy with this product ...

    1 answer 1

    About the "two identical goods."

    Here you have a box of pencils, one for each color.
    You mix them up and take the first 5 pieces.
    Could it be that all 5 of you turned out to be red?

    About the "tell me better."

    In principle, this query is not the most optimal. And if the goods are tens of thousands or more, it will slow down. There are ways to speed up, but they are all very different, they depend on the specific case. So for now it is better to leave it as it is, and to be bothered when the request starts to run longer than 0.01 seconds.

    About "With this product also buy."

    In 99% of stores in this section put stale goods, which must quickly sell.
    If you want to do what is written, then - a surprise! - it is necessary to record all purchases in a separate table and look for the goods that were bought with it. If details are needed, the rule “one problem - one question” should be remembered.

    • Thanks for the answer. If it's not difficult for you, please show me how to speed up the request, in a better way in your opinion. I will be very grateful! - Dmitry
    • There are a couple of ways to alternative order by rand () - retvizan
    • @retvizan, Thank you. - Dmitriy