简体   繁体   English

如何使用Mysql DB中的AngularJs从两个表中获取数据

[英]How to get data from two table using AngularJs from Mysql DB

I want to fetch data from two different tables from mysql using Angular Js. 我想使用Angular Js从mysql的两个不同表中获取数据。 My tables are shown below: 我的表格如下所示:

Comment 评论

ID,User_id,Comment

User 用户

Id,Name,Email,Password

I want to display data with angluarJs in this format: 我想用AngluarJs以这种格式显示数据:

Username : comments

To get all comments you want to run the following statement: 要获取所有注释,您需要运行以下语句:

SELECT Name AS Username, Comment
FROM user, comments
WHERE user.id = comments.user_id

This is combining the two tables and matching the user_id from the comments table with the id from the users table. 这是将两个表组合在一起,并将注释表中的user_id与用户表中的id进行匹配。

In general, it's a bad practice to query databases directly using Angular. 通常,直接使用Angular查询数据库是一种不好的做法。 The recommended way of doing this is to expose the data via (RESTful) API's which in turn, perform the queries at the back-end and return the data in some format (eg, JSON) which Angular can then consume. 推荐的方法是通过(RESTful)API公开数据,该API随后在后端执行查询,然后以Angular可以使用的某种格式(例如JSON)返回数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM