我有3个表table_1,table_2和table_3。
我正在尝试通过table_1和table_2的连接在table_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_id,如果获得多于一行的行,我将无法在连接时将其值增加1(仅对所有行不起作用一次)
我也无法弄清楚如何获得xxcolumn,xycolumn值。
参考我用table_1,table_2,,table_3
创建了sql小提琴