如何从 JOINed 表的查询中获取所有字段值?

发布于 2024-08-05 15:39:54 字数 928 浏览 5 评论 0原文

我有这个基本查询:

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, then description.
  • 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 技术交流群。

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

发布评论

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

评论(3

娇妻 2024-08-12 15:39:54

好的,我找到了原因:

具有 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 scenario description 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

静谧幽蓝 2024-08-12 15:39:54

这应该有效:

SELECT d.description, o.code 
FROM order_positions o, article_descriptions d 
WHERE o.order_id = 1 AND d.article_id = o.article_id

This should work:

SELECT d.description, o.code 
FROM order_positions o, article_descriptions d 
WHERE o.order_id = 1 AND d.article_id = o.article_id
冰之心 2024-08-12 15:39:54

您确定没有使用 fetchOne () 错误地代替了 fetchRow()

你能发布你的PHP代码吗?

另一种可能性是在您的代码中您错过了逗号:

SELECT a b

SELECT a AS b

Are you sure you're not using fetchOne() by mistake instead of fetchRow()?

Could you post your PHP code?

Another possibility is that in your code you missed the comma:

SELECT a b

is the same as

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