3

3 つのテーブルがあります。usercarおよびuser_x_caruser_x_car車を所有するユーザーを保持します。user_idcar_id格納されます。次のように、車を所有していないユーザーを取得したい:

$car_owner = $this->db->select()->from('user_x_car')->get()->result();

for ($i = 0; $i < count($car_owners); $i++)
    $car_owner_id[$i] = $car_owner[$i]->user_id;

$non_car_owner = $this->db->select()->from('user')->where_not_in('id', $car_owner_id)->get()->result();

私が望むものは得られますが、最初の選択で選択されたIDの配列を作成して作成する途中のforループをバイパスする方法はありますか。選択した s の配列をuser_id直接取得する方法はありますか?

4

3 に答える 3

1

次のような2つのクエリでそれを行うことができます

最初のものは user_x_car テーブルからすべての ID を取得します

$temp1=array();
$temp=$this->db->distinct()->select('user_id')->get('user_x_car')->result_array();

次に、ユーザーテーブルから車を持っていないユーザーを取得します

foreach($temp as $each)
{
    array_push($temp1,$each['user_id']);
}
$rs=$this->db->where_not_in('id',$temp1)->get('user');
if($rs->num_rows()>0)
{
    $data=$rs->result_array();
    print_r($data);die;
}

$data は、車を持っていないすべてのユーザーを出力します。問題が発生した場合はお知らせください。

于 2013-09-20T07:16:12.063 に答える
1
function get_unread_notifications_ids()
    {
        //get unread notifications ids
        $this->db->select('GROUP_CONCAT(fknotification_id) as alll');
        $this->db->from("te_notification_status_tbl");
        $this->db->where('read_status',0);

        $ids=$this->db->get()->row();
        return $idss=str_replace(",","','",$ids->alll);
    }

2 番目の関数は次のようになります。

function get_unviewed_photos_events(){
        $idss = $this->get_unread_notifications_ids();

        $this->db->select('img.*',False);
        $this->db->from("te_notifications_tbl notif");
        $this->db->join('te_images_tbl img','img.id=notif.reference_id','LEFT OUTER');

        $this->db->where("notif.id IN('".$idss."')");
        $rslt = $this->db->get()->result_array();
        return $rslt;
    }
于 2014-10-27T07:27:15.223 に答える
0

クエリ

$non_car_owner = $this->db->query('SELECT user.*
FROM user LEFT JOIN user_x_car ON user_x_car.id=user.id
WHERE table2.id IS NULL')->result();

ここでは、テーブル user_x_car にないユーザー

foreach($non_car_owner as $user){
   echo $user->user_id;
}
于 2013-09-20T06:03:54.240 に答える