使用Codeigniter加入查询,其中condition返回所有行和行数

时间:2016-05-19 07:20:00

标签: mysql

我们有2个表user_detailsshipments 我们必须从order_id表中获取每个用户的所有shipments,其中shipment_count表中的user_details小于或等于4onb_status shipments表中的!='1'

user_details (table)

shipments (table)

output

2 个答案:

答案 0 :(得分:0)

您可能想尝试这个。 mysql查询:

  

选择u.user_email作为电子邮件,s.from_address选自from,s.to_address as   to,s.order_id,u.user_id from shipment s left join user_details u on   u.user_id = s.user_id其中u.shipment_count< = 4和s.onb_status   != 1;

$this->db->select('u.user_email as email, s.from_address as from, s.to_address as to, s.order_id, u.user_id');
$this->db->from('shipments as s');
$this->db->where('u.shipment_count <=', 4);
$this->db->where('s.onb_status !=', 1);
$this->db->join('user_details as u', ' u.user_id = s.user_id');

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

答案 1 :(得分:0)

模型

function get_data(){
$this->db->select('*')
$this->db->from("shipments AS t1");
$this->db->join("user_details AS t2", "t1.user_id = t2.user_id);
$query = $this->db->get();
return $query->result();
    if ($query->num_rows() >0){ 
        foreach ($query->result() as $data) {
            $results = $data;
        }
    return $results;
    }

控制器

function data()
{
    $data['results'] = $this->your model->get_data();
    $this->load->view('your view');
}

视图

  <table border="2">
       <thead>
            <th>email</th>
            <th>from</th>
            <th>to</th>
            <th>order_id</th>
            <th>order_id</th>

        </tr>
    </thead>
    <tbody>
        <?php
            foreach ($result as $data) :
        ?>

        <tr>
            <td><?php echo $data->email; ?></td> 
            <td><?php echo $data->from_address; ?></td>
            <td><?php echo $data->to_addres; ?></td>
            <td><?php echo $data->order_id; ?></td>
            <td><?php echo $data->user_id?></td>

        </tr>
        <?php
            endforeach;
        ?>
    </tbody>
</table>

我希望它有效