Mysql – automate the “on” statement from MySQL queries

join;MySQLquery

Background

In the process of QA/QC, I am writing lots of queries with multiple joins. I am using MySQL.

I would like to know how (if) I can simplify join statements, e.g. by setting default fields to join on.

My tables are named with a plural form of the object that they contain:

 names
 types
 actions
 surnames
 names_surnames

where names_surnames is a many-many lookup table

the primary keys are always id and the foreign keys are always, e.g.

 names.type_id
 names.action_id
 names_surnames.name_id
 names_surnames.surname_id

My joins would be something like

select names.col1 
from names join types on names.type_id = type.id
           join actions on names.action_id = actions.id;

Question

Is there a way to get MySQL to always assume that tables will be joined with on thistables.thattable_id = thattable.id?

Best Answer

How about creating views for joins that are used repeatedly ?

For example:

create view my_view as 
select names.id as name_id, names.type_id, names.action_id, ...
from names join types on names.type_id = type.id
           join actions on names.action_id = actions.id;