X
X
xxxfdd2020-12-05 22:19:00
Python
xxxfdd, 2020-12-05 22:19:00

How to count all my elements in sql?

I have an online store and I need when I click on the basket to display all the goods, but I need it to display, for example, not Apple, Apple, Apple, but Apple 3 pcs here is the code

if message.text == 'Посмотреть продажи!':
        if len(mas_user_id) > 0: можно не образать внимание
            con = sqlite3.connect("telebot00.db") присоеденение
            cur = con.cursor()
            count_back = 0
            order = 'Orders' это так таблица называеться

            back_result = cur.execute(f"""SELECT * FROM {order}""").fetchall() просто чтение данных из таблицы
            # Вывод результатов на экран
            c = 0
            for back_elem in back_result: проходим по данным 
               
                    count_back = count_back + 1
                    back = back + str(count_back) + ') '
                    back = back + str(back_elem[1])
                    back = back + ' @'
                    back = back + str(back_elem[2])
                    back = back + ', '
                    back = back + str(back_elem[4])

                    back = back + ': '
                    back = back + ', '
                    back = back + str(back_elem[3])
                    countere = Counter(back_elem[3].split())
                    print(countere)

                

                    back = back + ', '
                    back = back + str(back_elem[3])
            bot.send_message(message.from_user.id, back + '❗')
            back = ''

        else:
            bot.send_message(message.from_user.id, 'Пока-что продаж нету ')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Amoralny, 2020-12-05
@xxxfdd

Alternatively, baskets can be stored in one table, for example Orders, while storing the user id and the number of the product that was added, and it is desirable to add a primary key to delete items that were deleted by the user.
There will be something like:

+---+---------+-------------+-------+
| id | user_id | product_id | count |  
+---+---------+-------------+-------+

After that you can do this
SELECT count FROM Orders WHERE user_id = "user_id" AND product_id = "product_id ";

Thus, you will receive the quantity of a certain product.
So it will be possible to delete records, for example, if the product was deleted id is the primary key, but not the user id! There may be an easier way, but this is how I would do it.
DELETE FROM Orders WHERE id = "id";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question