Question

I'm looking for a way of doing this without listing out all of the fieldnames and using AS on each one. Maybe this is more of a mysql question. To be clear, I offer an example:

table1: users
+-id-+------name------+-preferred_moose_id-+-preferred_preparation_id-+
| 20 | Chester Chore  |          1         |             3            |
| 72 | George Garnish |          2         |             2            |
| 88 | Liver Laddykin |          2         |             3            |
+----+----------------+--------------------+--------------------------+

table2: moose_types
+-id-+------name------+-------size------+---------------diet--------------+
| 1  | Canadian Moose | Pretty Big      | Corn and snow                   |
| 2  | Red Moose      | Not as big      | Dolphins                        |
| 3  | Horses         | Size of a horse | Oats, cheese, okra, and farmers |
+----+----------------+-----------------+---------------------------------+

table3: preparation_types
+-id-+-------name-------+-difficulty-+
| 1  | Boiled           |     3      |
| 2  | Stuffed          |     5      |
| 3  | Butterflied      |     9      |
+----+------------------+------------+

If I have SQL like this:

SELECT users.*, moose_types.*, preparation_types.*
  FROM users
 INNER JOIN moose_types ON moose_types.id = users.preferred_moose_id
 INNER JOIN preparation_types ON preparation_types.id = users.preferred_preparation_id
 WHERE users.id = 88

And then I dump out the query, I end up with something like this:

+---diet---+-difficulty-+-id-+-id-+-id-+------name------+------name------+------name------+-preferred_moose_id-+-preferred_preparation_id-+-------size------+
| Dolphins |     9      | 88 | 88 | 88 | Liver Laddykin | Liver Laddykin | Liver Laddykin |         2          |             3            | Not as big      |
+----------+------------+----+----+----+----------------+----------------+----------------+--------------------+--------------------------+-----------------+

Notice the data being overwritten/duplicated. Liver Laddykin doesn't like to eat Liver Laddykinned Liver Laddykin, no matter if it's a difficulty of 9 or not. This is a safeguard intentionally done by CF, and I can kind-of understand why they did it this way. However, there has to be ways to avoid this. The first of which would be using the AS operator in the query and manually listing out all of the field names. BUT

Again, I iterate: I want to avoid listing out all of the fields manually and using AS on each one, as this would complicate things a little bit, unless it can be done without listing the fields and can be applied either by coldfusion, or done by MySQL. If there is NO solution, I'm fine with that, but I know I'm not a CF expert and certainly NOT a mySQL expert. Please feel free to point out errors or something I'm overlooking even if it seems trivial to you.

UPDATE Thanks so far to the commenters! Unfortunately, changing field names is not an option (though it would definitely work).

Clarification of actual question

What I'm looking for is some way that either ColdFusion or MySQL can yield me different field names (and thus differentiable) automatically or programmatically, without having to manually list out each of the fields (even trying to avoid using a cfloop, I know I'm making this hard), and declare it using AS.

I'm thinking that there may be some advanced SQL out there that I'm not aware of.

Was it helpful?

Solution

For ColdFusion your column names will need to be unique, there is no way around that.

However, listing out the field names and using AS on each one isn't so bad. With a quick loop or two you can dynamically generate the SQL (no need to explicitly last things out) and you will be all set!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top