gpt4 book ai didi

python - 合并不同的结果 SQL

转载 作者:行者123 更新时间:2023-12-01 06:22:52 25 4
gpt4 key购买 nike

所以我的问题如下:我有一个正在尝试查询的 MariaDB 数据库。我正在看实验数据。我想要的是将多个实验总结为 SQL 查询的一个结果行。

数据如下:

Experiment_ID | Antibiotic | Strain | Medium    | ...
1 Ampicillin E. coli TBauto ...
2 Ampicillin E. coli TB + IPTG

我想要得到什么:

Experiment_ID | Antibiotic | Strain | Medium               | ...
1 Ampicillin E. coli TBauto, TB + IPTG ...

我不关心 Experiment_ID,它只是为了明确我正在谈论两个不同的条目。

我已经尝试过

Select
tmp.*
From
((Select * from my_tbl Where ExperimentID = 1)
Union
(Select * from my_tbl Where ExperimentID = 2)) as tmp

最终我仍然得到 2 行结果。我也可以在我的 Python 代码中完成此操作。但我不想修改从数据库获取的结果。

我想我一定是在寻找错误的单词来合并这两个条目。所以请引导我走向正确的方向。

最佳答案

您似乎想要聚合:

select min(Experiment_ID) as Experiment_ID, Antibiotic, Strain,
group_concat(Medium order by Experiment_ID separator ', ')
from t
where Experiment_ID in (1, 2)
group by Antibiotic, Strain;

关于python - 合并不同的结果 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60283428/

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