如何输出使用别名的 MySQL 查询结果?

发布于 2024-11-27 22:59:37 字数 2782 浏览 1 评论 0原文

我有两个主 MySQL 表(个人资料和联系人)以及许多补充表(以 prm_ 为前缀)。它们通过 PHP 进行访问和操作。

在本例中,我正在查询配置文件表,我将在其中检索所有者 ID 和饲养员 ID。然后,这将根据保存了所有者和饲养员信息的联系人表进行引用。

我在另一个有关连接和别名的问题上得到了很大的帮助,我还得到了以下查询。不幸的是,我在实际回应结果方面遇到了巨大的困难。每个处理自连接和别名的站点都提供了可爱的查询示例 - 但然后跳到“以及此输出等”。怎么输出????

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

与此相结合的是以下 PHP:

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

除联系人(性别、颜色等)之外的所有详细信息均返回罚款。 $owner$breeder 变量为空。

任何帮助我解决这个问题的帮助都将非常感激。

编辑:我的最终工作查询:

SELECT ProfileOwnerID, ProfileBreederID, 
        ProfileGenderID, ProfileAdultColourID, ProfileColourModifierID, ProfileYearOfBirth, 
        ProfileYearOfDeath, ProfileLocalRegNumber, ProfileName,
        owner.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

然后我可以通过以下方式输出:

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

非常感谢所有人!

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.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以心动 2024-12-04 22:59:37

我猜您正在使用 mysql_fetch_array 或 mysql_fetch_assoc 函数从结果集中获取数组?

在这种情况下,您不能使用

$row['ownerContact.ContactFirstName']

PHP-Docs read< /a>:

如果结果的两列或多列具有相同的字段名称,则
最后一列优先。访问其他列
相同的名称,您必须使用列的数字索引或创建一个
列的别名。对于别名列,您无法访问
内容与原始列名称。

因此,您可以在 SQL 查询中使用 AS 为双倍行设置其他名称,或者使用编号索引来访问它们。


这可能如下所示:

在查询中使用 AS

在标准 SQL 查询中,结果集中的列的命名方式与它们的值所来自的列相同。有时,这可能是由于命名冲突而导致的问题。在查询中使用 AS 命令,您可以重命名结果集中的列:

SELECT something AS "something_else"
FROM your_table

这会将 something 列重命名为 something_else (您可以将 "" 引号去掉,但我认为这会使它更具可读性)。

使用数组的列索引

另一种方法是使用列索引而不是它们的名称。看一下这个查询:

SELECT first_name, last_name
FROM some_table

结果集将包含两列, 0 ==> First_name1 ==>姓氏。您可以使用这些数字来访问结果集中的列:

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

为了能够使用列索引,您需要使用 mysql_fetch_row 或 mysql_fetch_assoc -函数,它提供关联数组、数值数组或两者(“两者”是标准的)。

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).

翻了热茶 2024-12-04 22:59:37

您需要将 * 替换为您需要的数据,并且类似的数据也必须创建别名:
ownerContact.ContactFirstName 为owner_ContactFirstName

BreederContact.ContactFirstName 为 Breeder_ContactFirstName 。

像这样:

从配置文件中选择ownerContact.ContactFirstName作为owner_ContactFirstName,breederContact.ContactFirstName作为breeder_ContactFirstName加入ownerContact ...等,

您将这样写:

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

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']);
阪姬 2024-12-04 22:59:37

使用 php 访问行时无法指定表别名。如果您没有 2 个同名字段,则可以通过 $row['ContactFirstName'] 访问它。在这种情况下,无论第二个出现的 ContactFirstName 都会覆盖第一个。

将查询更改为使用字段别名,这样您就可以执行$owner = $row['Owner_ContactFirstName']
我不是 100% 确定的另一个选择是通过索引访问字段,而不是通过名称(例如 $owner=$row[11])。即使它有效,我也不建议这样做,如果稍微改变你的查询,你会遇到很多麻烦。

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.

独﹏钓一江月 2024-12-04 22:59:37

在外部选择上,您只有两个表:

(inner select) as ilv
contacts as breederContact

根本没有ownerContact

On outer select You have only two tables:

(inner select) as ilv
contacts as breederContact

there is no ownerContact at all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文