插入时列值递增1..选择join

edqdpe6u  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(191)

我有三张table表1,表2和表3。
我正试图用表1和表2的联接将数据插入表3(此表中已有一些行)。

table_3 has following columns:

unique_id  - its unique id needs to be increment by 1 from its last value
client_key - should get from table_1.client_key
xcolumn    - should get from table_2.xcolumn
xxcolumn   - this column value depend on table_3.xcolumn (below I mentioned which value should be here) 
         if table_3.xcolumn contains any of this value (purchase some things,purchase) xxcolumn should be PURCHASE 
         if table_3.xcolumn contains any of this value (cash advance,ATM,promo cashing) xxcolumn should be CASH
         if table_3.xcolumn contains any of this value (promo,promo deal) xxcolumn should be PROMO
         if table_3.xcolumn doest not contains any above value then xxcolumn should be OTHER
xycolumn   - this column value depend on table_3.xxcolumn (below I mentioned which value should be here)
         if table_3.xxcolumn is PURCHASE xycolumn should be 1 
         if table_3.xxcolumn is CASH xycolumn should be 2
         if table_3.xxcolumn is PROMO xycolumn should be 3
         if table_3.xxcolumn is OTHER xycolumn should be 4           
ycolumn    - should get from table_2.ycolumn

我尝试了以下插入查询:

insert into table_3(unique_id,client_key,xcolumn,xxcolumn,xycolumn,ycolumn)  
select (select max(unique_id) + 1 from 
table_3),table_1.client_key,table_2.xcolumn,'cash',1,table_2.ycolumn from 
table_2  inner join table_1 on table_2.client_id=table_1.client_id;

但是对于unique\u id,我不能在join时将其值增加1,如果得到多行(只增加一次,而不是所有行)
我也不知道如何得到xxcolumn,xycolumn值。
作为参考,我已经创建了sql fiddle,包括表1、表2、表3
http://sqlfiddle.com/#!9/8a48477/2号

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题