douhuan7862 2012-05-09 19:44
浏览 59
已采纳

没有从数据库返回属性?

I have two tables: references (top) and tweets (bottom):

tweet_id  class_id
------------------
2         1
5         1
6         2

tweet_id  text
------------------
1         foo
2         bar
3         baz
4         foobar
5         boobaz
6         foo bar
7         bar baz

I want to retrieve all info of tweets that are in the references table. The desired output is:

tweet_id  class_id  text
---------------------------
2         1         foo
5         1         foobaz
6         2         foo bar

I'm now using:

SELECT r.tweet_id, t.text, r.class_id
FROM tweets t 
LEFT JOIN `references` r ON (r.tweet_id = t.tweet_id) 
ORDER BY r.tweet_id ASC LIMIT 100

But this leaves me empty-handed when trying to fetch the tweet_id. The text, however, is displayed:

while($tweet = db_fetch_object($result)) {
  echo "$tweet->tweet_id $tweet->text"
}

In the resultset (checked via phpmyadmin), tweet_id and class_id for each record is NULL. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duangang79177 2012-05-09 20:18
    关注

    Nevermind :-) I should have LEFT JOIN'ed vice versa:

    SELECT r.tweet_id, r.class_id, t.text
    FROM `references` r
    LEFT JOIN tweets t ON (r.tweet_id = t.tweet_id) 
    ORDER BY r.tweet_id ASC LIMIT 100
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解