sql join 的问题

发布网友

我来回答

3个回答

热心网友

join就象楼上说的那样,但是看你的意思好像不是join,好像是union
那么首先确定两个表的表结构相同,也就是字段类型都一致
然后select 字段1,字段2 from table1 union all select 字段1,字段2 from table2

字段1和字段2是我自己写的,当然你要确定属性相同后可以选更多的字段,表结构相同的话,可以select *

select a.总人数,b.及格人数,c.不及格人数 from (select count(*) as 总人数 from table) a,(select count(*) as 及格人数 from table where 成绩=及格) b,(select count(*) as 不及格人数 from table where 成绩=不及格) c

热心网友

mysql中有left join,right join ,cross join等,mysql手册中关于join语法的介绍:
table_references:
table_reference [, table_reference] …

table_reference:
table_factor
| join_table

table_factor:
tbl_name [[AS] alias]
[{USE|IGNORE|FORCE} INDEX (key_list)]
| ( table_references )
| { OJ table_reference LEFT OUTER JOIN table_reference
ON conditional_expr }

join_table:
table_reference [INNER | CROSS] JOIN table_factor [join_condition]
| table_reference STRAIGHT_JOIN table_factor
| table_reference STRAIGHT_JOIN table_factor ON condition
| table_reference LEFT [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [LEFT [OUTER]] JOIN table_factor
| table_reference RIGHT [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [RIGHT [OUTER]] JOIN table_factor

join_condition:
ON conditional_expr
| USING (column_list)

例子:

SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c);

SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id=table2.id WHERE table2.id IS NULL;

补充:
例如有两张表,tb1和tb2,
tb1有num, tb2也有num,选出tb1.num=tb2.num的部分
mysql使用left join 或right join都可以:
select * from tb1 lef join tb2 on tb1.num=tb2.num;

热心网友

用 union all 比较好吧
比如表A中有字段 StudentID, StudentName,
表B中有字段TeacherID,TeacherName。
现在想把学生和教师的信息写在一张表里,列名为ID和Name,可以这样写
select StudentID as ID, StudentName as Name
from A
union all
select TeacherID, TeacherName
from B
不知道是不是你想要的结果
union all的作用就是把几张表的查询结果合并在一张表里

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com