Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

melx's avatar
Level 4

Select match records and unmatched records

Hello i want to select matched and Unmatched records and saved in table Example all match records status=1 and unmatch records status=0

Right Now I managed to selected the match records, but i did not managed to find the unmatch records what can i do to get both and save into another table

another table field format

   unit_id  :  status



$Matchrecord=DB::table('devices')
                                ->select('Devicenumber','sales.created_at as sdate')
                                ->Leftjoin('sales','devices.id','=','sales.unit_issue_id')
                                 ->where('devices.devicetype',1)
                                 ->where('sales.cancel_bill',0)
                                 ->whereDate('sales.created_at',Carbon::today())->get();
0 likes
15 replies
vincent15000's avatar

You need to share all useful information if you want us to help you.

What is the structure of the table ?

melx's avatar
Level 4

@vincent15000

Table Structure

Devices table

        Schema::create('devices', function (Blueprint $table) {
                    $table->id();
                    $table->string('Devicenumber');
                    $table->integer('devicetype');
                    $table->integer('devicebrand');
                    $table->integer('status')->default(0);
                    $table->timestamps();
                });

Sales table

           Schema::create('sales', function (Blueprint $table) {
                    $table->id();
                    $table->string('unit_issue_id');
                      .
                      .
                      .
                    $table->timestamps();
                   });

Third table(New table which after get the match and unmatch data so that i can takes that unit_ids and save base on status as said above)

         Schema::create('t3', function (Blueprint $table) {
                    $table->id();
                    $table->string('unit_id');
                    $table->integer('status')->default(0);
                    $table->timestamps();
                });
Snapey's avatar

dont see the problem? can status be something other than 1 or zero?

melx's avatar
Level 4

@Snapey the query above is return the matched list, but am failed to get un matched ,

Example i have the list like this

              1 456
              2 457
              3 458
              4 459
              5 461
              6 462
              7 463
              8 464
               
       let say `1 3 4 8` are matched, so the remain `id` are not match  so, in the third table i want to save all `ids` but for `ids` which are matched `status=1` will be 1 and un matched id `status=0`     
melx's avatar
Level 4

@Snapey can status be something other than 1 or zero? like what? string or

melx's avatar
Level 4

@Snapey

why not two queries? , its Ok

matched how? , means the data which you get after selected on that two tables(see my query above)

Snapey's avatar

@melx sorry that's not a good answer. You have three where statements. Say for example there are 1000 records in the database and 10 match this query (set A). Is set B the other 990?

Snapey's avatar

@melx so select all records and set the status value accordingly when you insert into second table

Snapey's avatar

im puzzled what could possibly be the requirement to duplicate what is already in the database

melx's avatar
Level 4

@Snapey but keep in mind that i need to record everyday,

Please or to participate in this conversation.