dsf4354353452 2011-06-22 04:30
浏览 29

应用左连接以使用其外键从多个表中提取数据

I have a table TblOrders which have two fields like FldOrderStatusId and FldInstrumentID as a foreign key from the tables called TblOrderStatus and TblInstrumentMasters respectively. Is that possible to left join for the table. The code is given below:

$find_filled_orders = $this->UserOrder->query(
    "Select distinct(FldOrderNumber) from TblOrders where FldOrderStatusId =12 ");
$res_order="";
$i=0;
foreach($find_filled_orders as $order_arr)
{
    if($i!=0)
    {
        $res_order.=",";
    }
    $res_order.="'".$order_arr['TblOrders']['FldOrderNumber']."'";
    $i++;               
 }
 $where_not_in="";
 if($i>=1)
 {              
     $where_not_in =  "AND FldOrderNumber NOT IN (".$res_order.")";
 }
 //debugbreak(); 
 $current_order = $this->UserOrder->query(
     "Select * from TblOrders where 1 ".$where_not_in.
     " group by FldOrderNumber order by FldSlNo  desc"); 

I want to apply left join at the last line query. Please guys help me out. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duan5362 2011-06-22 06:38
    关注

    Are you sure you're using Cake? O_o There's no need for raw sql for such a simple query. If your tables and key names followed Cake's conventions, Cake would do it all for you with just a couple chained methods in your controller.

    I'm not even really sure what you're trying to accomplish, but I think you just want to pull all the status of all of a user's unfilled instrument orders, or something?

    There is exactly 0 need to manually construct queries in Cake, let alone prep strings like that. Let the framework do the heavy lifting. If needed, $find_filled_orders could be written as a nested SELECT statement (for which Cake provides elegant support). Otherwise, all you need is to retool your tables / models so you can associate them properly and let Cake do the rest.

    You don't show how you want to call instruments, so I can't be more specific. But Cake will automatically generate joins for associated models if you define them and name everything properly. Imply a relationship between Orders and Users by adding a column in Orders called user_id. Same for instruments_id and status_id, and if desired, for table Users - order_id, etc. Cake "foreign key" columns don't have to actually be defined foreign key constrained in the db, or even contain any data. Make them all null if it pleases you.

    $current_order = $this->Order->find('all', array(
        'conditions'=>array(
            'Order.status_id != 12',
            'Order.status_id'=>'Status.id', 
            'Order.instrument_id'=>'Instrument.id'
            ),
        'fields'=>array('DISTINCT (Order.number) as number', 'name', 'other'),
        'group'=> 'Order.number',
        'order'=>'Order.fld_sl_no DESC'
        )
    );
    

    (Both group and DISTINCT -- in real life, no need for both. I put them both in for the sake of demonstrating.)

    http://book.cakephp.org/view/1002/Creating-Database-Tables

    http://book.cakephp.org/view/1030/Complex-Find-Conditions

    HTH. :)

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度