表_1
id | user_id | morning
1 | 1 | apple
table_2
id | user_id | afternoon
1 | 1 | orange
table_3
id | user_id | evening
1 | 1 | pineapple
这些表仅是示例,我想加入它们以返回json
对象,如:
{'user_id': 1, 'morning': 'apple', 'afternoon': 'orange', 'evening': 'pineapple'}.
查询:
database.select('*').
from('table_1').
join('table_2', 'user_id', 'user_id').
join('table_3', 'user_id', 'user_id').
then(function() {});
我正在将knexjs
用于节点expressjs
服务器,并且我认为这是联接多个表的方法。我以前从未使用过join,所以不确定是否可行。不幸的是,我得到了这个错误。
列引用“ user_id”不明确
请帮助我理解错误和出了什么问题。谢谢
答案 0 :(得分:0)
要解决列引用“ user_id”是模棱两可的错误,
app.get('/get_all_data', function(req, res) {
database.from('table_1').innerJoin('table_2', 'table_1.user_id', 'table_2.user_id').innerJoin('table_3', 'participants.athlete_id', 'table_3.athlete_id').then(function(data) {
res.send(data);
});
});
因为我在多个表中使用了相同的列名(这不是一个很好的例子),所以需要像“ table_1.user_id”一样在user_id前面添加表名。