简体   繁体   中英

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.

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 . However, my script resulted in the Tran_Type populated into the tran_no field.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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