There are 2 tables, a table with Contracts , connected 1 to many with the table Firms .
Contracts:
+----+--------+---------+ | id | firm_1 | firm_2 | +----+--------+---------+ | 1 | 1 | 2 | | 1 | 3 | 2 | | 1 | 1 | 1 | +----+--------+---------+ Firms:
+----+------+ | id | name | +----+------+ | 1 | ООО | | 2 | ИП | | 3 | ОАО | +----+------+ When displaying the Contracts table, it is necessary to output the company name id . Example output:
+----+--------+---------+ | id | firm_1 | firm_2 | +----+--------+---------+ | 1 | ООО | ИП | | 1 | ОАО | ИП | | 1 | ООО | ООО | +----+--------+---------+ At the moment, the query looks like this and works only for 1 field:
SELECT id , Firms.name, firm_2 FROM Contracts LEFT JOIN Firms ON Firms.id = Contracts.firm_1;