列依赖于mysql中的其他列

时间:2014-12-23 10:25:19

标签: html mysql jsp

我有2个表,结构如下

mysql> select * from customer;
+-------+-------+
| cname | ccode |
+-------+-------+
| Sumit | c1    |
| Amit  | c2    |
| Sunil | c3    |
+-------+-------+

mysql> select * from supplier;
+-------+-------+
| sname | scode |
+-------+-------+
| Suraj | s1    |
| Naresh| s2    |
| Parul | s3    |
+-------+-------+

在两个表的第三个表单值中,我已传递给选项标记,以便用户可以选择其中一个。 当数据存储在mysql中时,我会得到像bellow

这样的答案
+-------------+--------+------+
|account_name | amount | code |
+-------------+--------+------+
| Amit        | 100    | c2   |
| Parul       | 400    | s3   |
| Suraj       | 100    | s1   |
+-------------+--------+------+

该代码列需要自动填充,因为我没有在我的jsp表单中显示任何代码值。

有什么可以建议我的吗?

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话:) ...

当客户和供应商代码是唯一的时,您可以使用带有相同单词的union子句在两个表中进行搜索:

    insert into thirdtable (account_name,amount,code) values ('wujek',100,(select ccode code from customer where cname='Sumit' union  select scode code from supplier where sname='Sumit'));

    insert into thirdtable (account_name,amount,code) values ('wujek',100,(select ccode code from customer where cname='Parul' union  select scode code from supplier where sname='Parul'));