gpt4 book ai didi

mysql - 获取文件名记录

转载 作者:行者123 更新时间:2023-11-28 23:25:35 25 4
gpt4 key购买 nike

我需要从表 foto 中获取文件名对于第一张 table

tb_induk
----------------
id_induk | nomor_induk | id_burung | hen | cock
25 | IND003 | 2 | 91442 | 30441
26 | IND004 | 2 | 10464 | 40020

注意:列“hen”和“cock”是来自鸟类的“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

最佳答案

我认为您可以加入 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

关于mysql - 获取文件名记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39523274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com