0

I have several tables with a bunch of fields and I need a Query that includes ALL the fields from two tables (JOINs). The result should have consistent naming scheme for the fields, that survives when I add fields to the source table.

To always select all fields of a table I can use the * operator. But when I join two tables using the *, it prepends the fields that occur in both tables (and only those) with the table name.

SELECT kids.*, parents.* FROM parents INNER JOIN kids ON parents.ID = kids.ParentID;

gives me

kids.name, birthday, school, parents.name, address ...

When I add a birthday column to the parents table I get

kids.name, kids.birthday, school, parents.name, parents.birthday, address ...

And I have to update birthday to kids.birthday everywhere.

Is there a way to prepend all column names in the beginning? So I'd get

kids.name, kids.birthday, kids.school, parents.name, parents.address ...

in the first place?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 1
    Does this answer your question? [SQL select join: is it possible to prefix all columns as 'prefix.\*'?](https://stackoverflow.com/questions/329931/sql-select-join-is-it-possible-to-prefix-all-columns-as-prefix) –  Nov 24 '19 at 22:43
  • Don't use exact same field name in multiple tables. Name is a reserved word and should not be used as name for anything, nor should any other reserved word such as Date. Why would parents have a single birthday field anyway? Why even bother with parent birthdays? – June7 Nov 25 '19 at 00:06
  • It is obvioiusly a simplified version to make the point. –  Nov 25 '19 at 14:24

0 Answers0