简体   繁体   English

根据另一个条件插入表

[英]insert into a table based on condition in another

I have trying to build a table from a condition existing in another table. 我试图从另一个表中存在的条件构建表。 If the condition exists I want to pull data from a different table to populate the new table. 如果条件存在,我想从其他表中提取数据以填充新表。 Here is the SQL statements I am using, however it is populating the new table with the wrong data. 这是我正在使用的SQL语句,但是它使用错误的数据填充了新表。

INSERT INTO 2014TranstarPriceTnum (tran_no)
SELECT Tran_no      
  FROM Trans_Types_Updated 
WHERE 2014TranstarPriceTtype.`tran_type`=Trans_Types_Updated.`Tran_Type`;

I am looking in 2014TranstarPriceTtype for a Trans Type that exists for the and pull all the associated tran_no 's from Trans_Types_Updated and populate 2014TranstarTnum . 我期待在2014TranstarPriceTtype针对存在的一个跨类型和把所有相关的tran_no从的Trans_Types_Updated和填充2014TranstarTnum However, my script resulted in the Tran_Type populated into the tran_no field. 但是,我的脚本导致将Tran_Type填充到tran_no字段中。

2014TranstarPriceTtype
Tprice  tran_type   make    core    note    Updated
650 125C    BUICK   250 (P.H.B.)    2014-01-07
650 200C    BUICK   250 (P.H.B.)    2014-01-07
850 2004R   BUICK   350 (P.H.B.)    2014-01-07
650 350 Chev    250 (P.H.B.)    2014-01-07

Trans_Type_Updated Trans_Type_Updated

Tran_No Tran_Type       Make    Eng_size
T1014AA 125C            BUICK   2.5
T1006AA 125C            BUICK   2.5
T1363AA 2004R           BUICK   5.0
T1365AA 2004R           BUICK   5.0
T1310AA 200C            BUICK   3.8 (231)
T1318AA 200C            BUICK   5.0
T1427CA 350C            CHEVROLET   5.0
T1427AA 350C            CHEVROLET   5.0

Results in 2014TranstarPriceTnum 2014TranstarPriceTnum结果2014TranstarPriceTnum

tran_no Tprice  tran_type   make    eng_size    core_tote   note    updated
125C    \N  \N  \N  \N  \N  \N  \N
200C    \N  \N  \N  \N  \N  \N  \N
2004R   \N  \N  \N  \N  \N  \N  \N
350C    \N  \N  \N  \N  \N  \N  \N

I would appreciated any help. 我将不胜感激。

Thanks, 谢谢,

Tony Cripps 托尼·克里普斯

Hmm, not that clear, but I think you want something like that (not sure of your table names, you've got different versions). 嗯,不是很清楚,但是我想您想要这样的东西(不确定您的表名,您使用的版本不同)。

INSERT INTO 2014TranstarPriceTnum (tran_no)
SELECT ttu.Tran_no      
  FROM Trans_Type_Updated ttu
  INNER JOIN 2014TranstarPriceTtype tpt on tpt.tran_type = ttu.tran_type

see SqlFiddle 参见SqlFiddle

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

相关问题 根据另一个表的条件插入 sql 语句 - INSERT sql statement based on condition from another table 需要创建MySQL过程以根据条件从表中获取数据并将其插入另一个表中 - Need to Create MySQL Procedure for getting data from table based on condition and insert them into another table 如何根据日期条件(PHP-MySQL-Query)将一个表中的多行插入另一个表? - How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)? 如何创建基于条件的 INSERT 触发器,更新或插入另一个表? - How do i create an INSERT trigger that based on a condition, either updates or inserts into another table? 尝试根据其他表中的条件独立插入时,WHERE NOT EXISTS不起作用 - WHERE NOT EXISTS not working when trying to insert based independent on condition from another table Mysql从具有条件和限制的另一个表INSERT INTO - Mysql INSERT INTO from another table with condition and limit MySQL:仅在另一个表条件时插入……如果不是则重试 - MySQL : insert only if another table condition … AND retry if not 插入但仅在满足另一个表上的条件时 - Insert but only if a condition on another table is met 将值插入到基于另一个表的表中 - INSERT values into a table based on another table MySQL查询基于另一个表的条件 - MySQL query based on condition from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM