【问题标题】:MySQL how to select with replacement of column with result of another query?MySQL如何选择用另一个查询的结果替换列?
【发布时间】:2022-01-17 07:30:50
【问题描述】:

所以,我有一个看起来像这样的 db.measurements

Date Value equipement_id operator_id
20-03-12 0.45 1 1
20-03-15 0.36 2 1
20-03-19 0.39 1 3
20-04-02 0.37 1 2

还有一个看起来像这样的 db.operators 和 db.equipements

operator_id operator_name
1 John
2 Mat
3 Brad
4 Alfred
equipement_id equipement_model
1 Model-ABC
2 Model-XYZ
3 Model-SuperX
4 Model-Wifi

所以我需要进行查询以从 db.measurements 中选择所有数据,但将设备和操作员的“id”替换为各自的名称/型号。最好的方法是什么? 我可以做到SELECT * FROM db.measurements;,但由于表格更长,因此无法弄清楚在这种情况下如何使用 REPLACE。

无法弄清楚连接是如何工作的。此查询无效,在 mysql 工作台上不返回任何内容

SELECT date, value, db.operators.operator_name as operator_name 
FROM db.measurements 
INNER JOIN db.operators ON db.operators.operator_id = operator_id;

谢谢

【问题讨论】:

    标签: mysql database join mariadb


    【解决方案1】:

    以同样的方式再次加入db.equipements

    SELECT m.date, m.value, o.operator_name, e.equipement_model
    FROM db.measurements AS m
    INNER JOIN db.operators AS o ON o.operator_id = m.operator_id
    INNER JOIN db.equipements AS e ON e.equipement_id = m.equipement_id
    

    【讨论】:

    • 完美男人。有用!我做错了...谢谢!
    猜你喜欢
    • 2016-09-28
    • 1970-01-01
    • 2018-03-08
    • 2011-03-13
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 2012-10-03
    • 2015-12-22
    相关资源
    最近更新 更多