我们正在使用学术数据库
列表mata_kuliah
id
nm_mk
kode_mk
重视表格mata_kuliah
id | nm_mk | kode_mk
1 | algoritma | TI001
2 | databases | TI002
3 | java | TI003
4 | OOP | TI004
5 | theses | TI005
专栏表格
id_mhs
nm_mhs
npm_mhs
值表纸卷
id | nm_mhs | npm_mhs
1 | Andi | 18.14.1.0001
2 | Soraya | 18.14.1.0002
3 | Mumun | 18.14.1.0003
4 | Dede | 18.14.1.0004
5 | Udin | 18.14.1.0005
我们正在从以前的数据库迁移到新的数据库,结果我们必须为1个结果(学术结果)调用另一个表
nilai_lama列
id_nilai_lama
id_mk
id_mhs
nilai_mk
nilai_baru列
id_nilai_baru
id_mk
id_mhs
nilai_mk
列nilai_konversi
id_nilai_konversi
id_mk
npm_mhs
nilai_mk
重视Tabel nilai_lama
id_nilai_lama | id_mk | npm_mhs | nilai_mk
1 | 1 | 18.14.1.0001 | 3
2 | 1 | 18.14.1.0002 | 4
3 | 1 | 18.14.1.0003 | 3
4 | 1 | 18.14.1.0004 | 3
5 | 1 | 18.14.1.0005 | 4
值表nilai_baru
id_nilai_baru | id_mk | npm_mhs | nilai_mk
1 | 2 | 18.14.1.0001 | 3
2 | 2 | 18.14.1.0002 | 4
3 | 2 | 18.14.1.0003 | 3
4 | 2 | 18.14.1.0004 | 3
5 | 2 | 18.14.1.0005 | 4
6 | 1 | 18.14.1.0001 | 4 (same id_mk, npm_mhs but hihger nilai_mk)
重视价值观nilai_konversi
id_nilai_konversi | id_mk | npm_mhs | nilai_mk
1 | 3 | 18.14.1.0001 | 3
2 | 3 | 18.14.1.0002 | 4
3 | 3 | 18.14.1.0003 | 3
4 | 3 | 18.14.1.0004 | 3
5 | 3 | 18.14.1.0005 | 4
6 | 3 | 18.14.1.0003 | 4 (take course twice but diff result)
查询
select
a.id_mk,
b.nilai_mk,
b.npm_mhs
from
mata_kuliah a
left join
(
(select id_mk,max(nilai_mk),npm_mhs from nilai_lama group by id_mk)
or
(select id_mk,max(nilai_mk),npm_mhs from nilai_baru group by id_mk)
or
(select id_mk,max(nilai_mk),npm_mhs from nilai_konversi group by id_mk)
) b
on
a.id_mk=b.id_mk and npm_mhs=?
预期结果npm_mhs = 18.14.1.0001
id_mk | nilai_mk | npm_mhs
1 | 4 | 18.14.1.0001 (value in nilai_baru is higher)
2 | 3 | 18.14.1.0001
3 | 3 | 18.14.1.0001
预期结果npm_mhs = 18.14.1.0003
id_mk | nilai_mk | npm_mhs
1 | 3 | 18.14.1.0003
2 | 4 | 18.14.1.0003 (has twice result in same tabel)
3 | 3 | 18.14.1.0003
这可能吗?
答案 0 :(得分:0)
能否请您检查此查询?考虑到您的预期输出,输出中有些不匹配。但是,如果我猜在您的表nilai_baru中,所有id_mk的值为2,并且输出18.14.1.0003的结果如下所示,则该查询对您有效。
print_r($result);
// Maybe Object
// (
// [value:Maybe:private] =>
// )
var_dump($result->join());
// NULL
查询是-
1 | 3 | 18.14.1.0003
2 | 3 | 18.14.1.0003
3 | 4 | 18.14.1.0003 (has twice result in same tabel)