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

后端 未结 9 1764
你的背包
你的背包 2020-12-08 02:13

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 inv

9条回答
  •  情歌与酒
    2020-12-08 02:48

    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.

提交回复
热议问题