使用一行

时间:2017-04-19 14:56:15

标签: mysql sql

表1

[      id      -      title      -      content      -      class      ]
[      1       -        2        -         3         -        1        ]
[      1       -        2        -         3         -        2        ]

表2

[      id      -       data      -    permission     -      class      ]
[      a       -        b        -         c         -        1        ]
[      a       -        b        -         c         -        2        ]

如何在选择所有table1

时从table2中选择列data

我的查询是SELECT title, content FROM table1 WHERE class = 1

如何在此查询中选择列datapermission

3 个答案:

答案 0 :(得分:0)

假设关系基于类:

select table1.*,table2.data, table2.permission
from table1 
join table2 on table1.class = table2.class 
where table1.class = 1

答案 1 :(得分:0)

您可以使用简单的JOIN

执行此操作
Select  T1.*, T2.Data, T2.Permission
From    Table1  T1
Join    Table2  T2  On  T1.Class = T2.Class
Where   T1.Class = 1

答案 2 :(得分:0)

select t1.title, t1.content, t2.data from table1 t1 join table2 t2
on t1.class=t2.class