Home > Blockchain >  MySQL how to select with replacement of column with result of another query?
MySQL how to select with replacement of column with result of another query?

Time:12-14

So, i have a db.measurements that look like this

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

And a db.operators and db.equipements that look like this

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

So i need to do a query to select all the data from db.measurements but replacing the "id" of both equipement and operator with the respective name/model. What would be the best way to do it? I can do SELECT * FROM db.measurements; but cant figure it out how to use REPLACE in this case since tables are longer.

Cant figure how join works. This query is not valid and returs nothing on mysql workbench

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

Thanks

CodePudding user response:

Just join again with db.equipements the same way.

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
  • Related