I have to filter a set of ids which i need to keep in a specific order. The problem is, when the filter (in this case shops.sector) excludes some records, the ORDER BY FIELD syntax gets invalid. The error message is: “Column ‘id’ in order clause is ambiguous”
Here is the complete query:
SELECT DISTINCT `companies`.id FROM `companies`
LEFT OUTER JOIN `shops` ON shops.company_id = companies.id
WHERE (companies.id IN (96296,1,53585) AND shops.sector = 'Mobile')
ORDER BY FIELD(id, 96296,1,53585) LIMIT 0, 30;
Is there any way to keep the order without knowing the filtered result first? I want to keep this in only one query if possible.
you have two or more table with a column with the same name.
you must specify what id are you ordering by, like
companies.idorshops.id