MySQL implements the taste management function of dishes in the ordering system, which requires specific code examples
In an ordering system, the taste management of dishes is a very important function . In the MySQL database, we can manage the taste of dishes by creating corresponding tables and using appropriate query statements. The following is a simple example that introduces how to use MySQL to implement the dish taste management function of the ordering system.
First, we need to create two tables, one is the dishes table (dishes) and the other is the flavors table (flavors). The menu table will save the information of the dishes, including the name, price, etc. of the dishes; the taste table will save all the taste options.
-- 创建菜品表 CREATE TABLE dishes ( dish_id INT PRIMARY KEY, dish_name VARCHAR(255), price DECIMAL(10, 2) ); -- 创建口味表 CREATE TABLE flavours ( flavour_id INT PRIMARY KEY, flavour_name VARCHAR(255) );
Next, we need to add its own flavor options for each dish. This can be achieved by creating a join table (dish_flavor). The connection table will save the corresponding relationship between dishes and flavors.
-- 创建连接表 CREATE TABLE dish_flavour ( dish_id INT, flavour_id INT, PRIMARY KEY (dish_id, flavour_id), FOREIGN KEY (dish_id) REFERENCES dishes(dish_id), FOREIGN KEY (flavour_id) REFERENCES flavours(flavour_id) );
Now, we can insert some sample data into the menu table. Below is an example with three dishes and four flavor options.
-- 插入菜品数据 INSERT INTO dishes (dish_id, dish_name, price) VALUES (1, '宫保鸡丁', 28.0), (2, '鱼香肉丝', 32.0), (3, '水煮鱼', 38.0); -- 插入口味数据 INSERT INTO flavours (flavour_id, flavour_name) VALUES (1, '微辣'), (2, '中辣'), (3, '重辣'), (4, '不辣'); -- 插入菜品口味关系数据 INSERT INTO dish_flavour (dish_id, flavour_id) VALUES (1, 1), (1, 2), (2, 2), (2, 4), (3, 1), (3, 3), (3, 4);
Next, we can use relevant query statements to implement the management function of dish taste. Here are some example queries.
SELECT d.dish_name, f.flavour_name FROM dishes d JOIN dish_flavour df ON d.dish_id = df.dish_id JOIN flavours f ON df.flavour_id = f.flavour_id;
SELECT f.flavour_name FROM dishes d JOIN dish_flavour df ON d.dish_id = df.dish_id JOIN flavours f ON df.flavour_id = f.flavour_id WHERE d.dish_name = '宫保鸡丁';
SELECT d.dish_name FROM dishes d JOIN dish_flavour df ON d.dish_id = df.dish_id JOIN flavours f ON df.flavour_id = f.flavour_id WHERE f.flavour_name = '重辣';
Through the above example, we can see how to use MySQL to implement the dish taste management function of the ordering system. We created the dish table, taste table and connection table, and implemented the association and query of dishes and taste options through corresponding query statements. In practical applications, we can expand and optimize accordingly according to specific needs.
The above is the detailed content of MySQL implements the dish taste management function of the ordering system. For more information, please follow other related articles on the PHP Chinese website!