Paul
Paul

Reputation: 63

Join queries in Laravel

I'm having a lot of issues writing this query. Can someone please advise!

Select all from bookings table where

bookings.product_id = product.id

then based on the return, i want to say if a field in the returned results (created_at) is greater than "7" then return 1 otherwise return 0.

Upvotes: 0

Views: 312

Answers (2)

Swati Joshi
Swati Joshi

Reputation: 71

Assumption:- you wanted to write booking.product_id = product_id If my assumption is correct below answer might help you.

with temp as(
Select * from bookings where
bookings.product_id = product_id)
select case when temp.created_at > 7 then 1 else 0
        end as comparison
from temp;

Let me know if my assumption was incorrect so that I can give you right answer.

Upvotes: 1

Sameer Shaikh
Sameer Shaikh

Reputation: 7814

$data = DB::table('product')->join('bookings','bookings.product_id','=','product.id')->get();
if($data->created_at > 7){
  return 1;
}else{
  return 0;
}

Here is the query which will work for you

Upvotes: 2

Related Questions