简体   繁体   中英

SQL select show table if not same value from another table (Laravel)

This is my query, I tried this query it works.

SELECT * 
FROM conference_venue 
WHERE id_venue NOT IN (SELECT id_venue FROM submission_data WHERE id_submission = 1);

i want to display data in conference_venue. but I don't want to display data whose id_venue is the same as the submission_data table (same as id_venue whose id_submission is mentioned).

I'm trying to make a query for the laravel version, but it's a blank white screen with no errors.

DB::table('conference_venue')
    ->whereNotIn('id_venue', function($q){
      $q->select('id_venue')
      ->from('submission_data')
      ->where('id_submission', '=', 1);
    })->select('*')->get();

This query works when I try it in sql query console but fails when I try it with Laravel query builder.

You can try this:

DB::table('conference_venue')
->select('*')
->whereRaw(
'conference_venue.id_venue NOT IN (SELECT submission_data.id_venue FROM submission_data WHERE id_submission = 1)'
)

Or better yet, create a Model for conference_venue and submission_data (ie: ConferenceVenue , SubmissionData ) and you can add Eloquent relationships for ConferenceVenue and SubmissionData .

Eloquent relationships, which supports a variety of common relationships (One To One, One To Many, Many To Many, etc.), are defined as methods on your Eloquent model classes. Since relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities. (reference: https://laravel.com/docs/8.x/eloquent-relationships )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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