如何从 JOINed 表的查询中获取所有字段值?
我有这个基本查询:
SELECT d.description, o.code FROM order_positions AS o
LEFT JOIN article_descriptions AS d ON (o.article_id = d.article_id)
WHERE o.order_id = 1
我正在使用 PEAR
中的 MDB2
来执行它并读取返回值。
但不知何故,结果数组总是包含 order_positions 表中的字段仅!,即结果数组看起来像这样,
row[code] = 'abc123'
而我希望它看起来像这样
row[description] = 'my description'
row[code] = 'abc123'
我已经尝试了以下操作:
- 改变字段的顺序,即首先是
code
,然后是description
。 - 改变连接表的顺序。
- 使用完整的表名而不是别名。
- 改用“MySQL join”(
SELECT FROM table1, table2 WHERE table1.id = table2.id
) - 使用带或不带
AS
的别名。
其他一些事实:
- 在 MySQL 查询浏览器中执行此查询工作正常,所有字段都会返回。
- 无论如何,
order_positions
表似乎是首选。当加入其他表时,我仍然只从该表中获取字段。
I have this elementary query:
SELECT d.description, o.code FROM order_positions AS o
LEFT JOIN article_descriptions AS d ON (o.article_id = d.article_id)
WHERE o.order_id = 1
and I'm using MDB2
from PEAR
to execute it and read the return values.
But somehow the result array always contains fields from the order_positions
table only!, i.e. the result array looks like this
row[code] = 'abc123'
while I want it to look like this
row[description] = 'my description'
row[code] = 'abc123'
I already tried the following:
- Vary the order of the fields, i.e.
code
first, thendescription
. - Vary the order of the joined tables.
- Used full table names instead of aliases.
- Used the "MySQL join" instead (
SELECT FROM table1, table2 WHERE table1.id = table2.id
) - Used aliases with and without
AS
.
Some other facts:
- Executing this query in MySQL Query Browser works fine, all fields are returned.
- The
order_positions
table seems to be preferred, no matter what. When joining with additional tables I still only get fields from this table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我找到了原因:
具有
NULL
值的字段未添加到数组中。在我的测试场景中,description
实际上为 null,因此在数组中不可用。我仍然会保留这个(尴尬的)问题,以防将来其他人遇到这个问题。
面掌 http://www.scienceblogs.de/frischer-wind /picard-facepalm-thumb-512x409.jpg
OK, I found the cause:
Fields with
NULL
values are not added to the array. In my test scenariodescription
was in fact null and hence was not available in the array.I'll still keep this (embarrassing) question, just in case someone else has this problem in the future.
Facepalm http://www.scienceblogs.de/frischer-wind/picard-facepalm-thumb-512x409.jpg
这应该有效:
This should work:
您确定没有使用
fetchOne ()
错误地代替了fetchRow()
?你能发布你的PHP代码吗?
另一种可能性是在您的代码中您错过了逗号:
与
Are you sure you're not using
fetchOne()
by mistake instead offetchRow()
?Could you post your PHP code?
Another possibility is that in your code you missed the comma:
is the same as