dqjl0906 2011-07-21 14:40
浏览 8

一次从2张桌子获取信息?

I need to get info from 2 tables in one query, I searched for it on google and it came up with joins? I got this code

mysql_query("SELECT code, url FROM apilinks, links WHERE apilinks.code='$code' and links.code='$code'");

but it doesn't seem to work, it outpus this mysql error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/codyl/public_html/projects/tests/tirl/code.php on line 18

I need to have it find url and code, in both the links table and the apilinks table, because the url has a get variable, and 'apilinks' is setup differently than 'links' so it will have the same looking url but it has to check both tables

  • 写回答

4条回答 默认 最新

  • doujingke4981 2011-07-21 14:42
    关注

    I don't think your error is from your SQL query, but anyway:

    Your SQL query generates a cartesian product. Every row in apilinks is joined with every row in links. This is rarely what you want. You need to add a JOIN condition to the WHERE clause.

    From your query, I guess code is the column you want to join on.

    SELECT links.code, url 
    FROM apilinks, links 
    WHERE apilinks.code=links.code
    AND links.code='$code'
    

    Or with using an explicit join, which may be more obvious to you:

    SELECT links.code, url 
    FROM apilinks INNER JOIN links ON apilinks.code=links.code
    WHERE links.code='$code'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据