Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a join, how to prefix all column names with the table it came from

Tags:

sql

mysql

I'm analysing a rather horrible legacy database/codebase, trying to reduce server load by combining queries into joins (including an email alert cron job that typically invokes well over a million separate queries).

SELECT * FROM  class_alerts_holding ah  INNER JOIN class_listings l ON l.id = ah.lid  INNER JOIN class_users u ON u.id = ah.uid LEFT JOIN class_prodimages pi ON pi.pid = ah.lid 

This spits out 120 columns...

aid | id | lid | uid | oid | catName | searchtext | alertfreq | listType | id | owner | title | section | shortDescription | description | featured | price | display | hitcount | dateadded | expiration | url | notified | searchcount | repliedcount | pBold | pHighlighted | notes | ... 

To assist my analysis of how to construct the new queries it would be awesome if I could prefix the columns in the result with the table they came from in the JOIN e.g.

class_alerts_holding.aid | class_alerts_holding.id | class_listings.lid | ... 

Is there a way to achieve this?

like image 773
Jarrod Smith Avatar asked Oct 31 '12 07:10

Jarrod Smith


People also ask

When the table is JOINed with itself the type of join is called?

A self join is a join in which a table is joined with itself (which is also called Unary relationships), especially when the table has a FOREIGN KEY which references its own PRIMARY KEY.

Which type of join is used to returns all from a table even when there are no matching rows?

A LEFT JOIN will always include the rows from the LEFT table, even if there are no matching rows in the table it is JOINed with. When there is no match, the corresponding rows will use NULL to represent the missing values from the second table.

Which join will return all rows from both tables where there is a match?

SQL FULL OUTER JOIN Example Note: The FULL OUTER JOIN keyword returns all matching records from both tables whether the other table matches or not.

Which join returns records when the values match in the joining tables?

An INNER JOIN is such type of join that returns all rows from both the participating tables where the key record of one table is equal to the key records of another table. This type of join required a comparison operator to match rows from the participating tables based on a common field or column of both the tables.


2 Answers

You could

select ah.*, l.*, u.*, pi.* from ... 

then the columns will be returned ordered by table at least.

For better distinction between every two sets of columns, you could also add "delimiter" columns like this:

select ah.*, ':', l.*, ':', u.*, ':', pi.* from ... 

(Edited to remove explicit aliases as unnecessary, see comments.)

like image 151
koljaTM Avatar answered Oct 13 '22 04:10

koljaTM


You could name the fields in your query and give them aliases:

SELECT     ah.whateverfield1 AS 'ah_field1',            ah.whateverfield2 AS 'ah_field2',            l.whateverfield3 AS 'l.field3',            [....] FROM       class_alerts_holding ah  INNER JOIN class_listings l ON l.id = ah.lid  INNER JOIN class_users u ON u.id = ah.uid LEFT JOIN  class_prodimages pi ON pi.pid = ah.lid 

Its a bit of work to manually set up if you have that many fields, but you can simplify this with this query...

SHOW FULL FIELDS FROM your_table_name; 

...and a good text editor and copy & paste.

like image 35
Bjoern Avatar answered Oct 13 '22 02:10

Bjoern