I have 5 tables. One primary and 4 additional (they have different columns).
- objects
- obj_mobiles
- obj_tablets
- obj_computers
Here is the structure of my main table (objects).
ID | type | name | etc…
So what I want to do is to join objects with other (obj_mobiles,obj_tablets,…) tables, depending on type field.
I know that I should use dynamic SQL. But I can’t make procedure. I think it should look like something like this.
SELECT objects.type into @tbl FROM objects;
PREPARE stmnt FROM "SELECT * FROM objects AS object LEFT JOIN @tbl AS info ON object.id = info.obj_id";
EXECUTE stmnt;
DEALLOCATE PREPARE stmnt;
Aslo pseudo-code
SELECT * FROM objects LEFT JOIN [objects.type] ON ...
Can anyone post procedure? Also I want to have all rows not just 1 row.
Thanks.
If you want all rows (bulk output) and not one row at a time, the below should be fast and also the output for all rows will contain all columns.
Lets consider below to be the fields of the tables.
obj_mobiles – ID | M1 | M2
obj_tablets – ID | T1 | T2
obj_computers – ID | C1 | C2
objects – ID | type | name | etc.,
The table are create as below.
also to confirm the datatypes of the columns are same as the original columns, the result is saved into a table and datatypes are checked below.