简体   繁体   English

使用CodeIgniter联接多个表

[英]Joining Multiple Tables using CodeIgniter

I'm needing some help with pulling data from multiple tables ( 3 total ). 我需要一些帮助从多个表中提取数据(共3个)。 I know there is the JOIN option, but I'm still having some trouble. 我知道有JOIN选项,但是我仍然遇到一些麻烦。 Not sure if I can JOIN the tables in one query to get my results. 不知道是否可以在一个查询中联接表以获取结果。 I'll try to explain what I'm doing before I post my attempted code... 在发布尝试的代码之前,我将尝试解释自己在做什么。

I'm trying to make an "Inventory Need" section on my site. 我正在尝试在我的网站上创建“库存需求”部分。 I've got an INVENTORY_PRODUCTS table where I check to see if the column UNITSINSTOCK is less than the MINLEVEL qty. 我有一个INVENTORY_PRODUCTS表,在该表中检查UNITSINSTOCK列是否小于MINLEVEL数量。 Pretty simple. 很简单 But I also need to check and see if that product is "On Order". 但是我还需要检查一下该产品是否“已订购”。 To do that I need to check my INVENTORY_ORDERS AND INVENTORY_ORDER_DETAILS tables. 为此,我需要检查我的INVENTORY_ORDERSINVENTORY_ORDER_DETAILS表。

The status of the order is in the INVENTORY_ORDERS table ( if it's 1 or NULL ) and then the details for the orders are in INVENTORY_ORDER_DETAILS . 订单的状态在INVENTORY_ORDERS表中(如果为1NULL ),然后订单的详细信息在INVENTORY_ORDER_DETAILS So I will need the SUM for that item if it exists. 因此,如果存在该项目,我将需要SUM

My HTML table has the following columns: Product #, Min, Max, Stock, On Order and Need . 我的HTML表包含以下列: Product #, Min, Max, Stock, On Order and Need

This is what I'm trying to use: 这就是我要使用的:

    function get_inventory_below_need($mfctId) {

    $this->db->select('
        INVENTORY_PRODUCTS.ID, INVENTORY_PRODUCTS.PRODUCTNUMBER, INVENTORY_PRODUCTS.MINLEVEL, INVENTORY_PRODUCTS.MAXLEVEL, INVENTORY_PRODUCTS.UNITSINSTOCK,
        SUM(INVENTORY_ORDER_DETAILS.QUANTITY) AS ORDERQTY
    ');
    $this->db->from('INVENTORY_PRODUCTS');
    $this->db->join('INVENTORY_ORDER_DETAILS', 'INVENTORY_ORDER_DETAILS.PRODUCTID = INVENTORY_PRODUCTS.ID', 'INNER');
    $this->db->join('INVENTORY_ORDERS', 'INVENTORY_ORDERs.ID = INVENTORY_ORDER_DETAILS.ORDERID', 'LEFT');
    $this->db->where('INVENTORY_PRODUCTS.MANUFACTURERID', $mfctId);
    $this->db->where('INVENTORY_ORDERS.STATUS != 9');
    $this->db->where('INVENTORY_PRODUCTS.UNITSINSTOCK < INVENTORY_PRODUCTS.MINLEVEL');
    $this->db->group_by('INVENTORY_PRODUCTS.PRODUCTNUMBER');
    $this->db->order_by("INVENTORY_PRODUCTS.PRODUCTNUMBER", "asc");

    $query = $this->db->get();

    return $query->result();
}

The problem is it's only displaying items that have a stock level below the min level AND are currently being ordered. 问题在于它仅显示库存水平低于最低水平并且当前正在订购的商品。 But I've got some products that are below min level and not being ordered. 但是我有一些低于最低水平并且没有被订购的产品。

Hope all this makes sense! 希望所有这些都有道理!

$query = $this->db->query('enter sql query here');

确定您将必须键入要使用的查询,但是它看起来可能会更好,并且如果您使用的是PHP MyAdmin或使用另一种访问数据库的方法,则可以对其进行测试。

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

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