使用laravel和join的sql查询(2个表)
你好 我想为我的Laravel项目创建一个SQL查询。 给你解释一下我的两张桌子 我有一张“产品”表。 在这个表中,我有一个主键>产品id。 我还有一个“状态”栏,指定产品id是“待售”还是“待租” 我还有第二张表“产品标准”。 在此表中,主键为>产品\标识 我有一个“criteres_id”列,它必须是=“30” 因此,我希望将“产品标准”表中的所有产品id=“30”和“状态”列与“产品”表中的“待售”相等 我试过这样的方法,但没有效果:使用laravel和join的sql查询(2个表),sql,laravel,Sql,Laravel,你好 我想为我的Laravel项目创建一个SQL查询。 给你解释一下我的两张桌子 我有一张“产品”表。 在这个表中,我有一个主键>产品id。 我还有一个“状态”栏,指定产品id是“待售”还是“待租” 我还有第二张表“产品标准”。 在此表中,主键为>产品\标识 我有一个“criteres_id”列,它必须是=“30” 因此,我希望将“产品标准”表中的所有产品id=“30”和“状态”列与“产品”表中的“待售”相等 我试过这样的方法,但没有效果: SELECT products_main_criter
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"
范例
$products\U sql=products::join('products\U details'、'products.id'、'='、'products\U details.id')
->其中(‘产品质量’、‘良好’)
->get()
我知道你想把这些表格联系起来,但事情是这样的
创建变量调用与表相关的表,并使用table.column创建一个“where”,并放置您只希望结果具有的vaslor此代码返回具有
产品id=30
和状态=“待售”
的产品:
products::where('status','for sale')
->whereIn('products_id',function($query){
$query->select('products_id')->from('products_criteres')
->where('criteres_id',30)
})->get()
使用此函数查询回调函数
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"
$products_sql = products::where('status','for sale')
->whereIn('products_id',function($q){
$q->select('products_id')->from('products_criteres')
->where('criteres_id',30)})->get();