-2

我想填表E,我有这些表。

表A

+------+--------+--------+
| A_ID | A_NAME | A_TYPE |
+------+--------+--------+
|   1  |  apple |    1   |
+------+--------+--------+

表B

+------+---------+--------+
| B_ID | B_NAME  | FROM_A |
+------+---------+--------+
|   1  | A_NAME  | A_ID   |
+------+---------+--------+

表C

+-------+------------+
|  C_ID |   C_NAME   |
+-------+------------+
| A_TYPE|    fruit   |
+-------+------------+
| A_TYPE| vegetables |
+-------+------------+

表D

+------+---------+
| D_ID | D_NAME  |
+------+---------+
|   1  |  fruit  |
+------+---------+

表E

+------+--------+
| E_ID | E_NAME |
+------+--------+
| B_ID |  D_ID  |
+------+--------+

我如何填写and , where the TableEwith D_Name, and between and no key, only through and , where the is key.TableB B_IDTableD D_IDC_Name =TableCTableDTableATableBA_ID

因此,如果 apple 是水果,请在 and 中写入TableE一条B_IDSQLD_ID语句。我怎样才能做到这一点?

我在 TableA 和 TableB 中有 2000 行我想将 C_Name 与 D_Name 进行比较,如果相同,则在 TableE 中写入来自 TableB 的 ID 和来自 TableD 的 ID。

4

1 回答 1

0
  INSERT INTO TableE (E_ID , E_NAME)
  SELECT b.B_ID ,d.D_ID FROM TableA a 
  INNER JOIN TableB b ON b.FROM_A = a.A_ID
  INNER JOIN TableC c ON c.C_ID  = a.A_TYPE
  INNER JOIN TableD d ON  d.D_NAME = c.C_NAME
于 2013-08-06T09:22:13.973 回答