Re: Query for two fields with the same name
| From: | Richard Lynch | Date: | Thu, 20 Apr 2006 06:40:59 +0000 |
| Subject: | Re: Query for two fields with the same name | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-234323@lists.php.net to get a copy of this message | ||
On Wed, April 19, 2006 5:55 pm, Kevin Murphy wrote:
> $connection = "select * from connection, pr WHERE connection.area_id
> = '$gateway_link' and connection.pr_id = pr.id";
>
> Both tables have a field called ID but they have different
> information in them. $row['id'] gets me the one from the table called
> "pr" but I need to call the ID from the table connection.
You have now stumbled into ONE of many reasons why "SELECT *" is just
Bad Code. :-)
Figure out which fields you REALLY need and specify them by name in
place of *
In addition, to actually solve your problem, you'll be able to do
something like:
SELECT pr.id as pr_id, connection.id as connection_id, area_id, ...
You can use " as XYZ" to sort of "rename" any column you want on the
fly, and that is what it will be called.
It's also really handy when you have formulas in your select:
SELECT (x.size + y.size) as sum, x.id, y.id from x, y ...
Without the "as sum" $row['something goes here'] will have your sum
answer, but what will it be called?
I don't even know the answer to that one, cuz whatever it is, it's
just too painful compared to using 'sum'
--
Like Music?
http://l-i-e.com/artists.htm