简体   繁体   English

带大小写的数据插入临时mysql表

[英]Inserting data into temporary mysql table with case

Consider the following example table: 考虑以下示例表:

CREATE TABLE items
    (`id` int, `productName` varchar(7), `price_new` decimal(10,2), `price_used` varchar(55))
;

INSERT INTO items
    (`id`, `productName`, `price_new`, `price_used`)
VALUES
    (1, 'cup', 10.50, 5.50),
    (2, 'plate', 9.50, 4.50),
    (3, 'mug', 8.50, 3.50)
;

For an application I wish to create a temporary table based on what the user has. 对于应用程序,我希望根据用户拥有的内容创建一个临时表。 The user would have say a 'cup' with condition of 'new'. 使用者会说条件为“新”的“杯子”。 (stored in a php array) (存储在php数组中)

My desired temporary table would be something like: 我想要的临时表如下所示:

CREATE TEMPORARY TABLE IF NOT EXISTS tempTable (
         `id` INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
          `name` varchar(7) NULL,
          `itemCondition` varchar(7) NULL,
          `price` decimal(10,2) NULL
        );

My question is this: If I create this temporary table how could I insert 'cup' and 'new' with MySQL selecting the price from the items table based on the itemCondition? 我的问题是这样的:如果我创建此临时表,如何在MySQL中基于itemCondition从项目表中选择价格来插入“ cup”和“ new”? psuedocode: 伪代码:

Insert: 
(1, 'cup', (
    if itemCondition == 'new' 
    (select Items.price_new where items.productName = 'cup')
    ELSE 
    (select Items.price_used where items.productName = 'cup') 

Thank you for your assistance. 谢谢您的帮助。

The problem is that until you insert the data into the temptable, sql will not know the value of the imtemCondition field. 问题在于,除非您将数据插入到临时表中,否则sql不会知道imtemCondition字段的值。 The value 'new' is supplied in the insert statement itself as a fixed value. 插入语句本身将值“ new”作为固定值提供。

You will need either stored procedure that selects the price based on a parameter value and then insert that price into the temporary table, or you can implement the same logic using php. 您将需要一个存储过程,该存储过程根据参数值选择价格,然后将该价格插入临时表中,或者您可以使用php实现相同的逻辑。

Steps: 脚步:

  1. Provide user id, name, and item condition. 提供用户标识,名称和项目条件。
  2. Select the price based on the item condition. 根据项目条件选择价格。
  3. Insert all data to the temporary table. 将所有数据插入临时表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM