【发布时间】:2018-01-03 21:28:42
【问题描述】:
在 Laravel 中,我有一个查询是:
$productsModel->join('tags', 'tags.item_id', '=', 'products.id')
->select('products.*', 'tags.item_id')
->distinct();
DB tables looks like this:
| products | tags |
----------|---------|
| id | id |
| | item_id |
products 与标签具有一对多的关系,这在连接表时会产生问题。例如,我需要根据多个标签查询产品,但我只需要显示一个产品。
【问题讨论】:
-
您的查询和数据库“模式”不匹配。
item_id是table_a还是table_b?$model是来自table_a或table_b的记录的实例吗?你想要与众不同的是什么? A 还是 B? -
谢谢。拼写错误 - 已更新。
-
查询一个产品的条件是什么?