如何输出使用别名的 MySQL 查询结果?
我有两个主 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜您正在使用 mysql_fetch_array 或 mysql_fetch_assoc 函数从结果集中获取数组?
在这种情况下,您不能使用
PHP-Docs read< /a>:
因此,您可以在 SQL 查询中使用
AS
为双倍行设置其他名称,或者使用编号索引来访问它们。这可能如下所示:
在查询中使用
AS
在标准 SQL 查询中,结果集中的列的命名方式与它们的值所来自的列相同。有时,这可能是由于命名冲突而导致的问题。在查询中使用
AS
命令,您可以重命名结果集中的列:这会将
something
列重命名为something_else
(您可以将""
引号去掉,但我认为这会使它更具可读性)。使用数组的列索引
另一种方法是使用列索引而不是它们的名称。看一下这个查询:
结果集将包含两列,
0 ==> First_name
和1 ==>姓氏
。您可以使用这些数字来访问结果集中的列:为了能够使用列索引,您需要使用 mysql_fetch_row 或 mysql_fetch_assoc -函数,它提供关联数组、数值数组或两者(“两者”是标准的)。
I guess you're using the
mysql_fetch_array
or themysql_fetch_assoc
-functions to get the array from the result-set?In this case, you can't use
as the PHP-Docs read:
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 QueryIn 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:This will rename the
something
-column tosomething_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:
The result-set will contain two columns,
0 ==> first_name
and1 ==> last_name
. You can use this numbers to access the column in your result-set:To be able to use the column-index, you'll need to use
mysql_fetch_row
or themysql_fetch_assoc
-function, which offers an associative array, a numeric array, or both ("both" is standard).您需要将 * 替换为您需要的数据,并且类似的数据也必须创建别名:
ownerContact.ContactFirstName 为owner_ContactFirstName
和
BreederContact.ContactFirstName 为 Breeder_ContactFirstName 。
像这样:
从配置文件中选择ownerContact.ContactFirstName作为owner_ContactFirstName,breederContact.ContactFirstName作为breeder_ContactFirstName加入ownerContact ...等,
您将这样写:
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 :
使用 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 whateverContactFirstName
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.在外部选择上,您只有两个表:
根本没有ownerContact
On outer select You have only two tables:
there is no ownerContact at all