Languages
[Edit]
EN

MySQL - create SELECT-JOIN query with column name prefixes

9 points
Created by:
JustMike
28980

In this article we would like to show how to create MySQL SELECT-JOIN query with column prefixes.

It is imposible to create direct query with column prefixes but there is some trick that let us to generate column names with aliases that can be combined into SELECT-JOIN query.

Note: it is required to have access to `information_schema`.`columns` table.

With this article you will be able to generate your query in following form:

SELECT
    `my_table_1`.`column_1` AS "my_table_1.column_1",
    `my_table_1`.`column_2` AS "my_table_1.column_2",
    `my_table_1`.`column_3` AS "my_table_1.column_3",
    `my_table_2`.`column_1` AS "my_table_2.column_1",
    `my_table_2`.`column_2` AS "my_table_2.column_2",
    `my_table_3`.`column_1` AS "my_table_3.column_1",
    `my_table_3`.`column_2` AS "my_table_3.column_2",
    `my_table_3`.`column_3` AS "my_table_3.column_3",
    `my_table_N`.`column_1` AS "my_table_N.column_1",
    `my_table_N`.`column_2` AS "my_table_N.column_2"
FROM `my_table_1`
/* JOIN here ... --> `my_table_2,my_table_3,my_table_N */
/* WHERE here ... */

 

Automatic query generator

In this section you can see example query generator, just set @database, @from_table and @join_tables to see effect.

/* QUERY CONFIGURATION */

SET
	@database:= "my_database",
	@from_table:= "my_table_1",
	@join_tables:= "my_table_2,my_table_3,my_table_N";

/* QUERY GENERATOR */

SELECT
	CONCAT(
		"SELECT\n",
		GROUP_CONCAT(
			"\t",
			CONCAT(
				`table_name`, ".", `column_name`,
				" AS ",
				CHAR(34), `table_name`, ".", `column_name`, CHAR(34)
			)
		SEPARATOR ",\n"), "\n",
		"FROM ", @from_table, "\n",
		"/* JOIN here ... --> ", @join_tables, " */\n",
		"/* WHERE here ... */\n"
	) "query"
FROM
	`information_schema`.`columns`
WHERE
		`table_schema` = @database
	AND
		FIND_IN_SET(`table_name`, CONCAT(@from_table, ',', @join_tables));

Example output:

SELECT
    `my_table_1`.`id`       AS "my_table_1.id",
    `my_table_1`.`username` AS "my_table_1.username",
    `my_table_1`.`email`    AS "my_table_1.email",
    `my_table_2`.`id`       AS "my_table_2.id",
    `my_table_2`.`message`  AS "my_table_2.message",
    `my_table_3`.`id`       AS "my_table_3.id",
    `my_table_3`.`file`     AS "my_table_3.file",
    `my_table_3`.`path`     AS "my_table_3.path",
    `my_table_N`.`id`       AS "my_table_N.id",
    `my_table_N`.`alert`    AS "my_table_N.alert"
FROM `my_table_1`
/* JOIN here ... --> `my_table_2,my_table_3,my_table_N */
/* WHERE here ... */

 

Manual query creation

In this section you can see example query that generates columns that can be used to create query.

Column prefixes can be generated with:

SELECT
	CONCAT(
		`table_name`, ".", `column_name`,
		" AS ",
		CHAR(34), `table_name`, ".", `column_name`, CHAR(34)
	) "field_names"
FROM
	`information_schema`.`columns`
WHERE
		`table_schema` = "my_database"
	AND
		`table_name` IN (
			"my_table_1",
			"my_table_2",
			"my_table_3",
			"my_table_N"
		);

Where: my_database, my_table_1, my_table_2, my_table_3 and my_table_N should be replaced with used database and table names.

Example output:

| field_names                                  |
|----------------------------------------------|
| my_table_1.id AS "my_table_1.id"             |
| my_table_1.username AS "my_table_1.username" |
| my_table_1.email AS "my_table_1.email"       |
| my_table_2.id AS "my_table_2.id"             |
| my_table_2.message AS "my_table_2.message"   |
| my_table_3.id AS "my_table_3.id"             |
| my_table_3.file AS "my_table_3.file"         |
| my_table_3.path AS "my_table_3.path"         |
| my_table_N.id AS "my_table_N.id"             |
| my_table_N.alert AS "my_table_N.alert"       |

Created SELECT-JOIN query from example output:

SELECT
    `my_table_1`.`id`       AS "my_table_1.id",
    `my_table_1`.`username` AS "my_table_1.username",
    `my_table_1`.`email`    AS "my_table_1.email",
    `my_table_2`.`id`       AS "my_table_2.id",
    `my_table_2`.`message`  AS "my_table_2.message",
    `my_table_3`.`id`       AS "my_table_3.id",
    `my_table_3`.`file`     AS "my_table_3.file",
    `my_table_3`.`path`     AS "my_table_3.path",
    `my_table_N`.`id`       AS "my_table_N.id",
    `my_table_N`.`alert`    AS "my_table_N.alert"
FROM `my_table_1`
JOIN `my_table_2` /* ON ... */
JOIN `my_table_3` /* ON ... */
JOIN `my_table_N` /* ON ... */
Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join