dongyin2643 2016-08-04 19:43
浏览 36

加入三个表mysql php,但没有回音

What did I do wrong. It echo nothing. The expect output is apple and pear with val good and 0. First, I will get all fruit with price 4 on table_1, then I join table_2, exclude any fruit mark=4, exclude banana, then I join table_3, get the val if factor match 56, so apple match, and pear factor did not match, so return 0. But my code echo nothing, didnot see error, but just echo nothing.

/*
table_1
fruit  price
apple   4
apple   5
banana  4
banana  5
pear    4

table_2
fruit   mark
apple    5
apple    6
banana   4
banana   7
pear     6

table_3
fruit   factor  val
apple    56     good
apple    60      OK
banana   89     good
banana   90     good
pear     56      bad
*/
$pri=4;
$sql = $wpdb->get_results( $wpdb->prepare("
                 SELECT 
                 b.fruit,c.val
                 FROM table_1 a,
                 LEFT JOIN table_2 b
                  ON b.fruit=a.fruit and b.mark != '4'
                 LEFT JOIN table_3 c
                  ON c.fruit=a.fruit AND c.factor = '56'
                WHERE a.price=%d
                 ",$pri));
print_r(sql);

</div>
  • 写回答

1条回答 默认 最新

  • dsfjnxjlbqv9812 2016-08-04 20:11
    关注

    Just Remove (,) after FROM (SELECT DISTINCT fruit FROM table_1 WHERE price=%d) a,

     /*table_1
     fruit  price
     apple   4
     apple   5
     banana  4
     banana  5
     pear    4
    
    table_2
    fruit   mark
    apple    5
    apple    6
    banana   4
    banana   7
    pear     6
    
    table_3
    fruit   factor  val
    apple    56     good
    apple    60      OK
    banana   89     good
    banana   90     good
    pear     56      bad
    */
    $pri=4;
    $sql = $wpdb->get_results( $wpdb->prepare("
                     SELECT 
                     b.fruit,c.val
                     FROM (SELECT DISTINCT fruit FROM table_1 WHERE price=%d) a
                     LEFT JOIN table_2 b
                      ON b.fruit=a.fruit and b.mark != '4'
                     LEFT JOIN table_3 c
                      ON c.fruit=a.fruit AND c.factor = '56'
                     ",$pri));
    print_r(sql);
    

    And the output is:

    fruit   val     
    apple   good
    apple   good
    pear    bad
    banana  NULL
    
    评论

报告相同问题?

悬赏问题

  • ¥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 海浪数据 南海地区海况数据,波浪数据