1

Bonjour,

I would like to create an SQL query for my Laravel project. To explain you a little bit my 2 tables.

I have a "products" table. In this table, I have a primary key > products_id. I also have a "status" column that specifies if the products_id is "for sale" or "for rent".

I have a second table "products_criteres". In this table, the primary key is > products_id I have a "criteres_id" column that must be = "30".

So I would like to get all the products_id that are = "30" in the "products_criteres" table and the "status" column to be equal to "for sale" in the "products" table.

I tried something like this, but nothing works:

SELECT products_main_criteres_history.products_id,date,criteres_id
FROM products_main_criteres_history LEFT JOIN products ON products_main_criteres_history.products_id = products_id
WHERE products_main_criteres_history.criteres_id = 30 
WHERE products.status = "a vendre"

4 Answers 4

1

Use this function query callback function

$products_sql = products::where('status','for sale')
                         ->whereIn('products_id',function($q){
                           $q->select('products_id')->from('products_criteres')
                         ->where('criteres_id',30)})->get();
Sign up to request clarification or add additional context in comments.

Comments

0

example

$products_sql = products::join('products_details', 'products.id', '=', 'products_details.id') ->where('products.quality', 'good') ->get();

I understood how you wanted to relate the tables but it’s something like this create the variable calls the table relating the tables and make a 'where' with table.column and put the vaslor that you only want the result to have

Comments

0
products::where('status','for sale')
->whereIn('products_id',function($query){
   $query->select('products_id')->from('products_criteres')
         ->where('criteres_id',30)
})->get()

Comments

0

this code returns products with criteres that have products_id = 30 and state = "for sale" :

SELECT pr.id,pr.date,prc.*
FROM products pr inner JOIN products_criteres prc ON pr.id = prc.products_id 
WHERE pr.products_id  = 30 
and pr.status = "for sale"

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.