mysql - Combining two tables in a complex way -
the situation:
i have main table, lets call maintable
.
+---------+----------+----------+----------+ | id (pk)| title | text | type | +---------+----------+----------+----------+ | 1 | text|more stuff| | | 2 | | example | b | +---------+----------+----------+----------+
and have second table called translationstable
, in id field representation of maintable
row id (no foreign key, can refering different tables), objtype objecttype (same name table), fieldname name of field objectype , value has translation value fieldname value in objtype table.
+---------+-----------+-----------+------------+----------+ | id | objtype | fieldname | value | language | +---------+-----------+-----------+------------+----------+ | 1 | maintable | title | algum texto| pt | | 1 | maintable | text | mais coisas| pt | +---------+-----------+-----------+------------+----------+
and because need search in translated fields, figured use temporary table
so, came problem of "which select query should use?". read posts pivot table queries, i don't know how can build query temp table like
+---------+------------+------------+----------+ | id (pk)| field_1 | field_2 | field_3 | +---------+------------+------------+----------+ | 1 | algum texto| mais coisas| | +---------+------------+------------+----------+
thank you.
edit:
i accepted ad7six answer because 500.000 entries in maintable , 1.500.000 in translations 30x times faster other one.
that's not complex
it's query 1 join per translated field.
that means query/sort/whatever other e.g. (using real names it's easier read):
select products.id, coalesce(product_name.value, products.name) name, coalesce(product_description.value, products.description) description products left join translationstable product_name on ( product_name.language = 'pt' , product_name.objecttype = 'products' , product_name.fieldname = 'name' , product_name.id = products.id ) left join translationstable product_description on ( product_description.language = 'pt' , product_description.objecttype = 'products' , product_description.fieldname = 'description' , product_description.id = products.id ) product_name.value = "algum texto" // find products named "algum texto"
you don't need temp table
but if want create one, it's easy using query itself:
create table products_pt select products.id, coalesce(product_name.value, products.name) name, coalesce(product_description.value, products.description) description ...
this create table (no indexes) matching structure of query. if data not change can make querying multilingual data lot easier manage, has disadvantages such (obviously) translation-specific table not date if source table data changes.
Comments
Post a Comment