MySQL JOIN:字符串不返回

发布于 2024-10-03 16:31:07 字数 1068 浏览 4 评论 0原文

我正在使用这个MySQL查询,由MySQL调用:

SELECT tf_posts.*, tf_threads.thread_id FROM tf_threads LEFT JOIN tf_posts ON tf_threads.thread_id=54

当我使用mysql_fetch_array时,我得到一个充满每个字段的数组,但这些字段的实际值确实表现得非常奇怪......

任何数字或日期字段都可以正常返回;我可以在数组中使用它们。然而,任何文本字段都是空的。返回的数组以其原始形式显示如下:

Array
(
    [0] => 
    [id] => 
    [1] => 
    [thread_id] => 820515612
    [2] => 
    [poster_id] => 
    [3] => 
    [title] => 
    [4] => 
    [body] => 
    [5] => 
    [date] => 
    [6] => 
    [edit_date] => 
    [7] => 
    [edited] => 
    [8] => 
    [draft] => 
    [9] => 
    [spam] => 
    [10] => 820515612
)

忽略此处的数字索引 - 我对命名的索引感兴趣。字段 bodytitle 是文本字段 (CHAR()),显然没有在应有的时候显示。

我在这里做错了什么或错过了什么?是因为我使用的是 CHAR() 吗?我强烈怀疑这一点,但我对 MySQL 不太熟悉。

编辑:

此查询的想法是从一个表 (tf_threads) 中选择所有线程,并从另一个表 (tf_posts) 中获取该线程下的第一篇帖子,并使用帖子 title 字段作为线程的标题。

谢谢,

詹姆斯

I'm using this MySQL query, called by MySQL:

SELECT tf_posts.*, tf_threads.thread_id FROM tf_threads LEFT JOIN tf_posts ON tf_threads.thread_id=54

When I use mysql_fetch_array I get an array full of each field, but the actual values for the fields are acting very strangely indeed...

Any numeric or date fields are returned just fine; I can use them in the array. Any text fields, however, are empty. The array returned is shown below in it's raw form:

Array
(
    [0] => 
    [id] => 
    [1] => 
    [thread_id] => 820515612
    [2] => 
    [poster_id] => 
    [3] => 
    [title] => 
    [4] => 
    [body] => 
    [5] => 
    [date] => 
    [6] => 
    [edit_date] => 
    [7] => 
    [edited] => 
    [8] => 
    [draft] => 
    [9] => 
    [spam] => 
    [10] => 820515612
)

Ignore the numerical indexes here - I'm interested in the named ones. The fields body and title are text fields (CHAR()), and are, obviously, not displaying when they should be.

What have I done wrong or missed here? Is it because I'm using CHAR()? I strongly doubt it but I'm not great with MySQL.

EDIT:

The idea of this query is to select all threads from one table (tf_threads), and take the first post under that thread from another table (tf_posts) and use the posts title field as the title for the thread.

Thanks,

James

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

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

发布评论

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

评论(2

鱼窥荷 2024-10-10 16:31:07

你的加入条款对我来说没有意义。看起来您正在尝试获取 thread_id 54 的所有帖子,但您没有加入这两个表,并且除了 thread_id 之外,您没有从 tf_threads 表中获取任何数据,我认为它也存在于您的 tf_posts 中桌子。为什么不只使用 WHERE 子句:

SELECT * FROM tf_posts WHERE thread_id = 54

如果这不起作用,请发布您的表描述以及查询应返回的内容。

要回答您的编辑,您需要实际连接表,如果您只想要一篇文章,则使用 INNER JOIN - LEFT JOIN 将为您提供每个线程的所有帖子。

SELECT tf_threads.*, tf_posts.* FROM tf_threads INNER JOIN tf_posts
   ON tf_threads.thread_id = tf_posts.thread_id
   ORDER BY tf_posts.date ASC

这应该为您提供所有主题以及每个主题的第一篇文章(由日期列确定)。

Your join clause doesn't make sense to me. It looks like you're trying to get all posts for thread_id 54, but you're not joining the two tables, and you're not getting any data from the tf_threads table other than thread_id, which I presume is also present in your tf_posts table. Why not just use a WHERE clause:

SELECT * FROM tf_posts WHERE thread_id = 54

If this doesn't work, please post your table descriptions and what the query should return.

To answer your edit, you need to actually join the tables then, and with an INNER JOIN if you only want one post - a LEFT JOIN would give you all posts for each thread.

SELECT tf_threads.*, tf_posts.* FROM tf_threads INNER JOIN tf_posts
   ON tf_threads.thread_id = tf_posts.thread_id
   ORDER BY tf_posts.date ASC

This should give you all threads and the first post (as determined by the date column) for each thread.

梦里°也失望 2024-10-10 16:31:07

SELECT tf_posts.*, tf_threads.thread_id FROM tf_threads LEFT JOIN tf_posts ON tf_threads.thread_id=54

看起来错误

ON 子句应该使用两个表中的字段将两个表连接在一起

SELECT tf_posts.*, tf_threads.thread_id
从 tf_threads 左连接 tf_posts ON tf_posts.thread_id = tf_threads.thread_id
WHERE tf_threads.thread_id = 54

正如 Thilo 所说,如果您已经有了线程 ID,只需使用它来通过 where 子句限制 posts 表

SELECT tf_posts.*, tf_threads.thread_id FROM tf_threads LEFT JOIN tf_posts ON tf_threads.thread_id=54

looks wrong

the ON clause should be joining the 2 tables together using fields in both tables

SELECT tf_posts.*, tf_threads.thread_id
FROM tf_threads LEFT JOIN tf_posts ON tf_posts.thread_id = tf_threads.thread_id
WHERE tf_threads.thread_id = 54

And as Thilo says if you already have the thread ID just use that to limit the posts table with a where clause

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