我有从表foto获取文件名的情况 对于firts表
tb_induk
----------------
id_induk | nomor_induk | id_burung | hen | cock
25 | IND003 | 2 | 91442 | 30441
26 | IND004 | 2 | 10464 | 40020

注意:专栏"母鸡"和"公鸡"是" id_data"来自鸟类
第二张表:
tb_foto
----------------------
id_foto | id_data | filename
566 |91442 |4b584a8cd6cb98d4b4d9614abb223034.JPG
567 |91442 |b526318215b5c12bf79b3284d8bd5db7.JPG
568 |30441 |4db159080e0cefb4fd9a2862502ab0f5.JPG
570 |10464 |4216d01abb3bdce90bd72e40ef1d593b.JPG
571 |40020 |6c5b28bffd79fc661f44733c04d12446.JPG

查询是:
Query
-----------------------
"select
tb_induk.id_induk as id_induk,
tb_induk.nomor_induk as nomor_induk,
case WHEN tb_foto.id_data=tb_induk.Hen THEN tb_foto.filename END as filenamehen,
case WHEN tb_foto.id_data=tb_induk.Cock THEN tb_foto.filename END as filenamecock
from tb_induk INNER JOIN tb_foto ON
tb_induk.Hen=tb_foto.id_data || tb_induk.Cock=tb_foto.id_data
where id_burung=$id_burung GROUP BY tb_induk.id_induk"

结果
Result
------------------------------
id_induk | nomor_induk | id_burung | hen | cock
25 | IND003 | 2 | 4216d01abb3bdce90bd72e40ef1d593b.JPG | NULL
26 | IND004 | 2 | NULL | 207b21895c556bc5fada1ead8ec34d06.JPG

答案 0 :(得分:1)
我认为您可以加入tb_foto
两次,为每个tb_induk
值提取母鸡和公鸡的文件名:
SELECT t1.tb_induk,
t1.nomor_induk,
t1.id_burung,
COALESCE(tf1.filename, 'NA') AS hen,
COALESCE(tf2.filename, 'NA') AS cock
FROM tb_induk t1
LEFT JOIN tb_foto tf1
ON t1.hen = tf1.id_data
LEFT JOIN tb_foto tf2
ON t1.cock = tf2.id_data