2014-09-19 91 views
0

嗨,大家好,我想知道爲什麼這個查詢不起作用。我很熟練使用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(沒有給出錯誤)

+0

什麼樣的錯誤/輸出給它? – Erik 2014-09-19 14:56:25

+0

沒有錯誤!沒有結果。用戶標識爲空任何其他參數填充 – voodo 2014-09-19 14:57:47

+0

您沒有選擇'user_id'。也許這就是原因。 – 2014-09-19 14:58:20

回答

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'"; 
+0

你很簡單:謝謝 – voodo 2014-09-19 15:09:45

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 
       )";