简体   繁体   中英

In MySQL is there a way to chose one column over another when doing SELECT *?

Ok, for a moment, throw out of your mind "good database design". Let's say I have two tables, and they have some of the same columns.

item
-------
id
title
color

and

item_detail
-------
id
weight
color

In a good normal query, you'd choose the columns you want within the query, like so:

SELECT item.title, item_detail.color, item_detail.weight ...

But what if you are stuck with a query that was built with star/all:

SELECT * ...

In this case you would get two color columns pulled back in your results, one for each table. Is there a way in MySQL to chose one color column over the other, so only one shows up in the results, without a full rewrite of the statement? So that I could say that the table item_detail takes priority?

Probably not but I thought I'd ask.

Err. No there is not.
But define "without a full rewrite of the statement". As far as I can see you'd just need to rewrite the select * portion of the query.
If you cannot touch the statement at all, then you are free to ignore the column in your application (the order of the columns does not change between calls)... or you could create a view...
It's hard to know which constraints you are dealing with when you say "But what if you are stuck with a query".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM