[英]MySQL Query should insert a second table into the first table if table1.id = table2.table1_id
I have two tables.我有两张桌子。
*brands b*
id, name, img
*models m*
id, name, img, brand_1
I would like to get the following output:我想得到以下 output:
Expeted result / output加速结果 / output
b.id,
b.name,
b.img,
models = m.id, m.name, m.img (where b.id = m.brand_id);
Do I have to do two steps for this?我必须为此做两个步骤吗?
m.brand_id = b.id
.迭代品牌并给我所有m.brand_id = b.id
的模型。I hope you understand what I mean.我希望你明白我的意思。 In the PHP Laravel Framework you can get one to many relationships as a collection.在 PHP Laravel 框架中,您可以将一对多关系作为一个集合。 Now my question: Can I solve this with a query?现在我的问题是:我可以通过查询来解决这个问题吗? And is my approach the right one at all?我的方法完全正确吗?
You can laravel always let you show the raw query that the quera builder constructs.您可以 laravel 始终让您显示 quera builder 构造的原始查询。
but basically you are searching for an INNER JOIN
但基本上你正在寻找一个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.