开发者

How to Output the results of a MySQL query that used aliases?

开发者 https://www.devze.com 2023-03-24 22:11 出处:网络
I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed by prm_). They are accessed and manipulated via PHP.

I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed by prm_). They are accessed and manipulated via PHP.

In this instance I am querying the profiles table where I will retrieve an Owner ID and a Breeder ID. This will then be referenced against the contacts table where the information on the Owners and Breeders is kept.

I received great help here on another question regarding joins and aliases, where I was also furnished with the following query. Unfortunately, I am having huge difficulty in actually echoing out the results. Every single site that deals with Self Joins and Aliases provide lovely examples of the queries - but then skip to "and this Outputs etc etc etc". How does it output????

SELECT *
FROM (
      SELECT *
         FROM profiles
              INNER JOIN prm_breedgender
                  ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
              LEFT JOIN contacts ownerContact
                  ON profiles.ProfileOwnerID = ownerContact.ContactID
              LEFT JOIN prm_breedcolour
                  ON profiles.ProfileAdultColourID = prm_breedcolour.BreedColourID
              LEFT JOIN prm_breedcolourmodifier
                  ON profiles.ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID
) ilv LEFT JOIN contacts breederContact
     ON ilv.ProfileBreederID = breederContact.ContactID
WHERE ProfileName != 'Unknown'
ORDER BY ilv.ProfileGenderID, ilv.ProfileName ASC $limit

Coupled with this is the following PHP:

$owner = ($row['ownerContact.ContactFirstName'] . ' ' . $row['ownerContact.ContactLastName']);
$breeder = ($row['breederContact.ContactFirstName'] . ' ' . $row['breederContact.ContactLastName']);

All details EXCEPT the contacts (gender, colour, etc.) return fine. The $owner and $breeder variables are empty.

Any help in settling this for me would be massively appreciated.

EDIT: My final WORKING query:

SELECT ProfileOwnerID, ProfileBreederID, 
        ProfileGenderID, ProfileAdultColourID, ProfileColourModifierID, ProfileYearOfBirth, 
        ProfileYearOfDeath, ProfileLocalRegNumber, ProfileName,
        owner开发者_Python百科.ContactFirstName AS owner_fname, owner.ContactLastName AS owner_lname,
        breeder.ContactFirstName AS breeder_fname, breeder.ContactLastName AS breeder_lname,
        BreedGender, BreedColour, BreedColourModifier

        FROM profiles
                    LEFT JOIN contacts AS owner
                        ON ProfileOwnerID = owner.ContactID
                    LEFT JOIN contacts AS breeder
                        ON ProfileBreederID = breeder.ContactID
            LEFT JOIN prm_breedgender
                        ON ProfileGenderID = prm_breedgender.BreedGenderID
                    LEFT JOIN prm_breedcolour
                        ON ProfileAdultColourID = prm_breedcolour.BreedColourID
                    LEFT JOIN prm_breedcolourmodifier
                        ON ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID

                  WHERE ProfileName != 'Unknown'
            ORDER BY ProfileGenderID, ProfileName ASC $limit

Which I could then output by:

$owner = ($row['owner_lname'] . ' - ' . $row['owner_fname']);

Many Thanks to All!


I guess you're using the mysql_fetch_array or the mysql_fetch_assoc-functions to get the array from the result-set?

In this case, you can't use

$row['ownerContact.ContactFirstName']

as the PHP-Docs read:

If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.

So, you can either use an AS in your SQL-query to set other names for the doubled rows or use the numbered indexes to access them.


This could then look like this:

Using AS in your Query

In your standard SQL-query, the columns in the result-set are named like the columns which their values come from. Sometimes, this can be a problem due to a naming-conflict. Using the AS-command in your query, you can rename a column in the result-set:

SELECT something AS "something_else"
FROM your_table

This will rename the something-column to something_else (you can leave the ""-quotes out, but I think it makes it more readable).

Using the column-indexes for the array

The other way to go is using the column-index instead of their names. Look at this query:

SELECT first_name, last_name
FROM some_table

The result-set will contain two columns, 0 ==> first_name and 1 ==> last_name. You can use this numbers to access the column in your result-set:

$row[0] // would be the "first_name"-column
$row[1] // would be the "last_name"-column

To be able to use the column-index, you'll need to use mysql_fetch_row or the mysql_fetch_assoc-function, which offers an associative array, a numeric array, or both ("both" is standard).


you need to replace the * with the data you need , and the similar ones you have to make aliases too : ownerContact.ContactFirstName as owner_ContactFirstName and breederContact.ContactFirstName as breeder_ContactFirstName .

like this :

select ownerContact.ContactFirstName as owner_ContactFirstName , breederContact.ContactFirstName as breeder_ContactFirstName from profiles join ownerContact ... etc

in this way you will write :

$owner = ($row['owner_ContactFirstName'] . ' ' . $row['owner_ContactLastName']);
$breeder = ($row['breeder_ContactFirstName'] . ' ' . $row['breeder_ContactLastName']);


You cannot specify table alias when you access row using php. Accessing it by $row['ContactFirstName'] would work if you didn't have 2 fields with the same name. In this case whatever ContactFirstName appears second overwrites the first.

Change your query to use fields aliases, so you can do $owner = $row['Owner_ContactFirstName'].
Another option I'm not 100% sure is to access field by index, not by name(e.g. $owner=$row[11]). Even if it works I don't recommend to do so, you will have a lot of troubles if change your query a bit.


On outer select You have only two tables:

(inner select) as ilv
contacts as breederContact

there is no ownerContact at all

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号