简体   繁体   English

MySQL:从大小写表达式向表中插入值

[英]MySQL: insert value into table from case expression

Below I have a procedure I run to insert some values into my shipin table. 下面,我运行一个程序,将一些值插入到shipin表中。 Typically I've had my Reference 2 field static, but I've since needed it to be variable. 通常,我的Reference 2字段是静态的,但此后我需要将其可变。 However, the field I'm pulling from, 'shipinimport'.'accountID' needs to return different results than what already exists. 但是,我从中提取的字段“ shipinimport”。“ accountID”需要返回与已经存在的结果不同的结果。 That's where the case expressions comes in as you can see. 如您所见,这就是case表达式出现的地方。

My problem is, this doesn't actually insert the returned results into my shipin table. 我的问题是,这实际上没有将返回的结果插入到我的shipin表中。 I can query it and get the results I'm looking for, but I need it to insert it into the table with the rest of the values in my procedure. 我可以查询它并获取所需的结果,但是我需要将其与过程中其余值一起插入表中。 I've tried moving the expression around in my procedure thinking it might be an order of events issue, but that didn't work. 我尝试过在过程中移动表达式,以为这可能是事件的顺序问题,但这没有用。 So is there a way to do this? 那么有没有办法做到这一点? Any help or guidance would be really appreciated. 任何帮助或指导将不胜感激。

CREATE DEFINER=`root`@`%` PROCEDURE `SubjectDetailsExportToShipIn_hs`()
BEGIN

INSERT INTO ShipInImport
SELECT *
FROM dpi_timestone.SubjectDetailsView_hs;

INSERT INTO `timestone`.`shipin`
     (`REFERENCE_1`,
     `REFERENCE_2`,
     `date_loaded`,
     `location`)

SELECT
     `shipinimport`.`SubjectKey`, --Reference 1
     `shipinimport`.`AccountID`, --Reference 2
      Now(), --DateLoaded
     'DPI'--location
FROM `dpi_timestone`.`shipinimport`;

Select Case AccountID
      When 16 Then 191
      When 1 Then 125
    End
FROM `dpi_timestone`.`shipinimport`;
End

If you're just trying to replace those AccountID values for "Reference 2"; 如果您只是想将那些AccountID值替换为“参考2”,请执行以下操作: put the CASE in your first select, like so: 将CASE放在您的首选中,如下所示:

SELECT
     `shipinimport`.`SubjectKey`, --Reference 1
      CASE `shipinimport`.`AccountID`
        WHEN 16 THEN 191
        WHEN 1 THEN 125
        ELSE `shipinimport`.`AccountID`
      END, --Reference 2
      Now(), --DateLoaded
     'DPI'--location
FROM `dpi_timestone`.`shipinimport`;

If that is not the case, your code does not make it obvious where you expect 191 and 125 to end up. 如果不是这种情况,则您的代码不会使您期望191和125结束的地方变得显而易见。

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

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