Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL join tables based on column data and table name

I'm wondering if this its even posible.

I want to join 2 tables based on the data of table 1. Example table 1 has column food with its data beeing "hotdog".

And I have a table called hotdog.

IS it possible to do a JOIN like.

SELECT * FROM table1 t join t.food on id = foodid

I know it doesnt work but, its even posible, is there a work arround?.

Thanks in advance.

like image 307
user1052347 Avatar asked Nov 17 '11 17:11

user1052347


People also ask

How do I join two tables based on a column?

To join two tables based on a column match without loosing any of the data from the left table, you would use a LEFT OUTER JOIN. Left outer joins are used when you want to get all the values from one table but only the records that match the left table from the right table.

How do I join two tables in different column names in SQL?

Merging tables by columns. Multiple tables can be merged by columns in SQL using joins. Joins merge two tables based on the specified columns (generally, the primary key of one table and a foreign key of the other).

How do you join tables based on conditions?

You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.

What are the 4 different table joining types?

Four types of joins: left, right, inner, and outer. In general, you'll only really need to use inner joins and left outer joins.


1 Answers

No, you can't join to a different table per row in table1, not even with dynamic SQL as @Cade Roux suggests.

You could join to the hotdog table for rows where food is 'hotdog' and join to other tables for other specific values of food.

SELECT * FROM table1 JOIN hotdog ON id = foodid WHERE food = 'hotdog'
UNION
SELECT * FROM table1 JOIN apples ON id = foodid WHERE food = 'apples'
UNION
SELECT * FROM table1 JOIN soups  ON id = foodid WHERE food = 'soup'
UNION 
...

This requires that you know all the distinct values of food, and that all the respective food tables have compatible columns so you can UNION them together.

What you're doing is called polymorphic associations. That is, the foreign key in table1 references rows in multiple "parent" tables, depending on the value in another column of table1. This is a common design mistake of relational database programmers.

For alternative solutions, see my answers to:

  • Possible to do a MySQL foreign key to one of two possible tables?
  • Why can you not have a foreign key in a polymorphic association?

I also cover solutions for polymorphic associations in my presentation Practical Object Oriented Models In SQL, and in my book SQL Antipatterns: Avoiding the Pitfalls of Database Programming.

like image 199
Bill Karwin Avatar answered Nov 15 '22 20:11

Bill Karwin