Home Database Mysql Tutorial How to use MySQL to create a user points record table for the grocery shopping system

How to use MySQL to create a user points record table for the grocery shopping system

Nov 01, 2023 pm 02:02 PM
user Record integral mysql creates points record table

How to use MySQL to create a user points record table for the grocery shopping system

The vegetable shopping system is a very practical application. It can easily provide users with fresh vegetables, fruits and other ingredients. In order to better serve users, user points need to be established Record form to track users’ consumption behavior and usage habits. This article will introduce how to use MySQL to create a user points record table for the grocery shopping system, and provide specific code examples.

1. Design of user points record table

The user points record table is a table used to record changes in user points. As users use the grocery shopping system to shop and consume, user points also increase. Corresponding changes will occur. Therefore, the following factors need to be considered when establishing a user points record table:

1. User information
2. Points change type (consumption, refund, activity, etc.)
3. Points change amount
4. Change time

Based on the above considerations, we can design the following user points record table:

##idint(11)Auto-increment primary keyuser_idint(11)userIDchange_typevarchar(20 )Points change type (consumption, refund, activity, etc.)change_amountint(11)Change amount change_timedatetimeChange time (format: YYYY-MM-DD HH:MM:SS)
Field name Data type Description
2. Specific code for creating user points record table

Creating a table in MySQL is very simple, just use the CREATE TABLE statement. The following is a sample code for creating a user points record table:

CREATE TABLE user_points (
  id int(11) NOT NULL AUTO_INCREMENT,
  user_id int(11) NOT NULL COMMENT '用户ID',
  change_type varchar(20) NOT NULL COMMENT '积分变化类型(消费、退款、活动等)',
  change_amount int(11) NOT NULL COMMENT '积分变化额度',
  change_time datetime NOT NULL COMMENT '变化时间(格式:YYYY-MM-DD HH:MM:SS)',
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户积分记录表';
Copy after login

In the above code, we used the CREATE TABLE statement to create a table named "user_points", which contains 4 fields. They are: id (auto-increment primary key), user_id (user ID), change_type (point change type), change_amount (point change amount) and change_time (change time). When creating, we also set the table to use the InnoDB storage engine and specified the encoding to be utf8mb4.

3. Add data to the user points record table

After creating the user points record table, we need to start entering data. The following is some sample data that can be used to test the correctness of the table:

INSERT INTO user_points (user_id, change_type, change_amount, change_time) 
VALUES 
(10001, '消费', -100, '2021-06-01 12:00:00'),
(10001, '消费', -50, '2021-06-03 18:00:00'),
(10001, '活动', 200, '2021-06-05 09:00:00'),
(10002, '消费', -80, '2021-06-02 15:00:00'),
(10002, '消费', -120, '2021-06-04 20:00:00'),
(10002, '活动', 150, '2021-06-06 13:00:00');
Copy after login

In the above code, we used the INSERT INTO statement to insert 6 pieces of data into the "user_points" table, so that It's time to start testing the correctness of the table.

4. Query the user points record table

Finally, we need to query the user points record table to verify the correctness of the table. The following are some common query statement examples:

1. Query the total points of a user:

SELECT SUM(change_amount) AS point_total FROM user_points WHERE user_id=10001;
Copy after login

In this statement, we use the SELECT SUM() statement to calculate the ID of The total points of 10001 users, and the result is returned with the alias "point_total".

2. Query the last 5 points change records of a user:

SELECT * FROM user_points WHERE user_id=10001 ORDER BY change_time DESC LIMIT 5;
Copy after login

In this statement, we use the SELECT * statement to query the last 5 changes of the user with ID 10001 Integral changes are recorded and sorted in reverse order of change time.

3. Query the score change records of all users within a certain period of time:

SELECT * FROM user_points WHERE change_time BETWEEN '2021-06-01 00:00:00' AND '2021-06-30 23:59:59';
Copy after login

In this statement, we used the SELECT * statement to query the points change records between June 1, 2021 and All points changes that occurred between June 30 are recorded.

To sum up, this article introduces how to use MySQL to create a user points record table for the grocery shopping system, and provides specific code examples. These codes can be used to test the correctness of the table and check the accuracy of the data through query statements, thereby completely establishing a complete user points recording system.

The above is the detailed content of How to use MySQL to create a user points record table for the grocery shopping system. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Xiaohongshu account to find users? Can I find my mobile phone number? How to use Xiaohongshu account to find users? Can I find my mobile phone number? Mar 22, 2024 am 08:40 AM

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

Log in to Ubuntu as superuser Log in to Ubuntu as superuser Mar 20, 2024 am 10:55 AM

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Mar 12, 2024 pm 07:20 PM

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

Explore Windows 11 guide: How to access user folders on your old hard drive Explore Windows 11 guide: How to access user folders on your old hard drive Sep 27, 2023 am 10:17 AM

Certain folders are not always accessible due to permissions, and in today’s guide we will show you how to access user folders on your old hard drive on Windows 11. The process is simple but can take a while, sometimes even hours, depending on the size of the drive, so be extra patient and follow the instructions in this guide closely. Why can't I access my user folders on my old hard drive? User folders are owned by another computer, so you cannot modify them. You don't have any permissions on the folder other than ownership. How to open user files on old hard drive? 1. Take ownership of the folder and change permissions Find the old user directory, right-click on it and select Properties. Navigate to "An

Tutorial: How to delete a normal user account in Ubuntu system? Tutorial: How to delete a normal user account in Ubuntu system? Jan 02, 2024 pm 12:34 PM

Many users have been added to the Ubuntu system. I want to delete the users that are no longer in use. How to delete them? Let’s take a look at the detailed tutorial below. 1. Open the terminal command line and use the userdel command to delete the specified user. Be sure to add the sudo permission command, as shown in the figure below. 2. When deleting, be sure to be in the administrator directory. Ordinary users do not have this permission. , as shown in the figure below 3. After the delete command is executed, how to judge whether it has been truly deleted? Next we use the cat command to open the passwd file, as shown in the figure below 4. We see that the deleted user information is no longer in the passwd file, which proves that the user has been deleted, as shown in the figure below 5. Then we enter the home file

Windows 11 KB5031455 fails to install, causing other issues for some users Windows 11 KB5031455 fails to install, causing other issues for some users Nov 01, 2023 am 08:17 AM

Microsoft began rolling out KB2 to the public as an optional update for Windows 503145511H22 or later. This is the first update to enable Windows 11 Moment 4 features by default, including Windows Copilot in supported areas, preview support for items in the Start menu, ungrouping of the taskbar, and more. Additionally, it fixes several Windows 11 bugs, including potential performance issues that caused memory leaks. But ironically, the optional update for September 2023 will be a disaster for users trying to install the update, or even for those who have already installed it. Many users will not install this Wi

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

Analysis of user password storage mechanism in Linux system Analysis of user password storage mechanism in Linux system Mar 20, 2024 pm 04:27 PM

Analysis of user password storage mechanism in Linux system In Linux system, the storage of user password is one of the very important security mechanisms. This article will analyze the storage mechanism of user passwords in Linux systems, including the encrypted storage of passwords, the password verification process, and how to securely manage user passwords. At the same time, specific code examples will be used to demonstrate the actual operation process of password storage. 1. Encrypted storage of passwords In Linux systems, user passwords are not stored in the system in plain text, but are encrypted and stored. L

See all articles