SQL连接在多个表中,并根据每一行进行连接

时间:2013-10-29 02:47:38

标签: sql

我需要从表A中获取值,表A,表A中的8行, 并使用表A中每行的id来从表B和表C中获取数据, 并将这些数据存储在一个数组中。

这就是我的意思:

Select id, name from table A --This returns 8 rows, which is the base table.

Select rate, review from table B --this returns 5 rows, and i store this 5 rows in an array and i append it to first row in table A

Select rate, review from table C --this returns 5 rows, and i store this 5 rows in an array and i append it to first row in table A

基本上, 我正在做一个需要从两个不同的表中获取行的请求, 并将其添加到该基表的第一行,因为我创建了8行基表。

当我将其作为json数据返回时,这就是它的外观:

"Rows": {
         "Rows0": {
             "ID": "524",
             "Name": "Value 1",
             "reviews": {
                 "name": "review for row value 0",
                 "timing": [
                     {
                         "day": "Sunday",
                         "Timing": "5:00PM"
                     },
                     {
                         "day": "Monday",
                         "Timing": "5:00PM"
                     },
                     {
                         "day": "Tuesday",
                         "Timing": "5:00PM"
                     }
                 ],
                 "day_showing": "Tuesday",
                 "recordid": "54570",
             }
         },
         "Rows1": {
             "ID": "789",
             "Name": "Value 1",
             "reviews": {
                 "name": "review for row value 1",
                 "timing": [
                     {
                         "day": "Sunday",
                         "Timing": "5:00PM"
                     },
                     {
                         "day": "Monday",
                         "Timing": "5:00PM"
                     },
                     {
                         "day": "Tuesday",
                         "Timing": "5:00PM"
                     }
                 ],
                 "day_showing": "Tuesday",
                 "recordid": "54570",
             }
         }
.....and more rows

我有没有办法使用连接,并在内部将这三个表连接在一起。 这样我就不必遍历每一行并附加两个表, 因为这段代码效率低下, 当我返回第一行时, 像8个不同的那样,8行被附加到第一行并从两个不同的表中作为数组返回。 我需要有人帮助更多地了解这一点。 我正在寻找最好的SQL代码构造,谢谢。

0 个答案:

没有答案