0

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.

6
  • which database are you using? mysql?oracle?mssql?other? Commented May 5, 2015 at 8:55
  • im using mysql - phpmyadmin Commented May 5, 2015 at 9:00
  • 1
    @Robson How does it matter? the DB query will be same Commented May 5, 2015 at 9:11
  • @GauravDave you are right, I was thinking about giving him the plain SQL query Commented May 5, 2015 at 10:39
  • @Robson Okay, but that wasn't useful in case of laravel. Commented May 5, 2015 at 10:40

2 Answers 2

2
$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

Sign up to request clarification or add additional context in comments.

4 Comments

This query seems to work but whenever i try to reference this in the blade view i get a Trying to get property of non-object error. In the blade view i have the if statement. The query is contained in my routes file and passes into the page where im trying to reference it
first of all writing a query in route is a super bad practice.what you wanna do is create a controller point your route to a function of that controller write the above code in the function and pass the data to the view using with.
i know but i can pass other variables through this and it isn't a problem. i just need it to reference the right field from the table but it is saying there is no object of any field name returned. is there any way i can check what the query returns?
before the if statement do this echo '<pre>'; print_r($data->toArray()); echo '<pre>'; exit;
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.