2010-11-20 118 views
0

我使用這個MySQL查詢,由MySQL叫:當我使用mysql_fetch_array我得到一個全陣列的每個字段的MySQL的JOIN:字符串不返回

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

,但對於字段中的實際值確實表現得非常奇怪......

任何數字或日期字段返回就好;我可以在數組中使用它們。但是,任何文本字段都是空的。返回的數組顯示如下爲原始形式:

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字段線程的標題。

感謝,

詹姆斯

回答

1

您的加入條款沒有道理給我。它看起來像你試圖讓thread_id 54的所有帖子,但你沒有加入這兩個表,並且你沒有從tf_threads表中得到除thread_id以外的任何數據,我認爲它也存在於你的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 

這應該會給你所有線程和每個線程的第一篇文章(由日期列確定)。

+0

請參閱我的編輯。謝謝。 – Bojangles 2010-11-20 13:01:54

+0

編輯我的答案。 – Thilo 2010-11-20 13:24:18

+0

這很好用 - 非常感謝你:-) – Bojangles 2010-11-20 13:49:51

0

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

看起來錯

ON子句應在兩個表中使用字段來連接2代表一起

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

正如蒂洛說,如果你已經擁有的線程ID只是用它來限制的職位表where子句