
[英]Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id
[英]MySQL Query should insert a second table into the first table if table1.id = table2.table1_id
我有兩張桌子。
*brands b*
id, name, img
*models m*
id, name, img, brand_1
我想得到以下 output:
加速結果 / output
b.id,
b.name,
b.img,
models = m.id, m.name, m.img (where b.id = m.brand_id);
我必須為此做兩個步驟嗎?
m.brand_id = b.id
的模型。我希望你明白我的意思。 在 PHP Laravel 框架中,您可以將一對多關系作為一個集合。 現在我的問題是:我可以通過查詢來解決這個問題嗎? 我的方法完全正確嗎?
您可以 laravel 始終讓您顯示 quera builder 構造的原始查詢。
但基本上你正在尋找一個INNER JOIN
SELECT
b.id,
b.name,
b.img,
m.id as models
, m.name
, m.img
FROM models m INNER JOIN brands ON b.id = m.brand_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.