How do I mysql select with aliases from another table?

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2010-03-08T20:13:16Z Indexed on 2010/03/08 20:21 UTC
Read the original article Hit count: 110

Filed under:

I'm working with a CMS system where I cannot control database column names. And I've got two related tables:

Table: content

+------------+----------+----------+----------+----------+
| content_id | column_1 | column_2 | column_3 | column_4 |
+------------+----------+----------+----------+----------+
|          1 |    stuff |     junk |     text |     info |
|          2 |    trash |     blah |     what |      bio |
+------------+----------+----------+----------+----------+

Table: column_names

+------------+-------------+
| column_id  | column_name |
+------------+-------------+
|          1 |   good_text |
|          2 |    bad_text |
|          3 |   blue_text |
|          4 |    red_text |
+------------+-------------+

What I'd like to do here is select from the first table, but select the columns AS the column_name from the second table. So my result would look like:

+------------+-----------+----------+-----------+----------+
| content_id | good_text | bad_text | blue_text | red_text |
+------------+-----------+----------+-----------+----------+
|          1 |     stuff |     junk |      text |     info |
|          2 |     trash |     blah |      what |      bio |
+------------+-----------+----------+-----------+----------+

© Stack Overflow or respective owner

Related posts about mysql