多列和表sql选择

时间:2014-09-19 14:54:17

标签: php mysql sql

大家好,我想知道为什么这个查询不会起作用。我非常不熟悉sql :)

$sql = "select o.item_id, o.order_id, o.qty, o.price 
         from " . mainframe()->table('order_items') . " o
         left join " . $wpdb->posts . " p
          on o.item_id = p.id
         where o.item_type = 'product'
         and (select j.user_id from ". mainframe()->table('orders') ." j where j.id = o.order_id)";

我需要获取另一个表上的用户ID并将其他结果加入

编辑:问题是它没有返回user_id(没有给出错误)

2 个答案:

答案 0 :(得分:1)

试试这个:

 $sql = "select 
             j.user_id as user_id, 
             o.item_id as item_id, 
             o.order_id as order_id, 
             o.qty as qty, 
             o.price as price
          from " . mainframe()->table('order_items') . " o
          left join " . $wpdb->posts . " p
              on o.item_id = p.id
          where 
                  o.item_type = 'product'
              and (
                    select j.user_id 
                    from ". mainframe()->table('orders') ." j 
                    where j.id = o.order_id
                  )";

答案 1 :(得分:0)

$sql = "select  o.item_id, o.order_id, o.qty, o.price, j.user_id 
         from " . mainframe()->table('order_items') . " o
         left join " . $wpdb->posts . " p
          on o.item_id = p.id
           left join ". mainframe()->table('orders') ." j onj.id = o.order_id
         where o.item_type = 'product'";