Mysql在运行时根据列动态复制行

时间:2017-11-03 09:54:38

标签: mysql sql

想象一下下面的表结构:

约会

+----+-------+
| id | name  |
+----+-------+
|  1 | test1 |
|  2 | test2 |
|  3 | test3 |
+----+-------+

appointment_carers

+----+----------------+--------+--------+--------+
| id | appointment_id | carer1 | carer2 | carer3 |
+----+----------------+--------+--------+--------+
|  1 |              1 |      1 |      2 |      3 |
|  2 |              2 |      4 |      5 |      6 |
+----+----------------+--------+--------+--------+

不幸的是,我现在无法改变这些表的现有结构。

有没有办法可以查询所有约会和每个看护人的插槽,我得到预约行的重复?

以下是查询的预期结果示例:

+----+-------+-------+
| id | name  | carer |
+----+-------+-------+
|  1 | test1 |     1 |
|  1 | test1 |     2 |
|  1 | test1 |     3 |
|  2 | test2 |     4 |
|  2 | test2 |     5 |
|  2 | test2 |     6 |
+----+-------+-------+

2 个答案:

答案 0 :(得分:3)

select id, name, carer from (
select appointment_id, carer1 as carer from appointment_carers
union
select appointment_id, carer2 as carer from appointment_carers
union
select appointment_id, carer3 as carer from appointment_carers
) as carers
join appointments on id = appointment_id;

答案 1 :(得分:1)

一种方法是glp_smcp smcpParm = new glp_smcp(); GLPK.glp_init_smcp(smcpParm); GLPK.glp_simplex(lp, smcpParm);

Problem   created 
GLPK Simplex Optimizer, v4.63
1 row, 4 columns, 4 non-zeros
      0: obj =   0.000000000e+00 inf =   1.231e+03 (1)
      1: obj =   1.231000000e+03 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
GLPK Integer Optimizer, v4.63
1 row, 4 columns, 4 non-zeros
4 integer variables, all of which are binary
Integer optimization begins...
+     1: mip =     not found yet >=              -inf        (1; 0)
Solution found by heuristic: 1600
+     2: >>>>>   1.400000000e+03 >=   1.400000000e+03   0.0% (1; 0)
+     2: mip =   1.400000000e+03 >=     tree is empty   0.0% (0; 1)
INTEGER OPTIMAL SOLUTION FOUND
z = 1231.0
x1 = 0.769375
x2 = 0.0
x3 = 0.0
x4 = 0.0