简体   繁体   English

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

[英]Apply left join to extract data from multiple tables using their foreign key

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. 我有一个表TblOrders,它有两个字段,如FldOrderStatusId和FldInstrumentID,分别作为来自名为TblOrderStatus和TblInstrumentMasters的表的外键。 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. 提前致谢。

Are you sure you're using Cake? 你确定你在使用Cake吗? O_o There's no need for raw sql for such a simple query. O_o对于这样一个简单的查询,不需要原始的sql。 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. 如果您的表和键名遵循Cake的约定,那么Cake会在您的控制器中使用几个链式方法为您完成所有操作。

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. 在Cake中只需要手动构建查询,更不用说准备这样的字符串了。 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). 如果需要,可以将$ find_filled_orders编写为嵌套的SELECT语句(Cake为其提供优雅的支持)。 Otherwise, all you need is to retool your tables / models so you can associate them properly and let Cake do the rest. 否则,你只需要重新调整你的表/模型,这样你就可以正确地关联它们,让Cake完成其余的工作。

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. 但是,如果你定义它们并且正确命名所有内容,Cake会自动为关联模型生成连接。 Imply a relationship between Orders and Users by adding a column in Orders called user_id. 通过在Orders中添加名为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. 对于instruments_id和status_id,如果需要,对于表Users- order_id等也是如此.Cake “外键”列实际上不必定义在db中约束的外键,甚至包含任何数据。 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.) (小组和DISTINCT - 在现实生活中,不需要两者。为了展示,我把它们都放进去。)

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

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

HTH. HTH。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM