The multi-table union query statement in mysql is: [select statement 1 union [union option] select statement 2 union [union option] select statement n]. Multi-table joint query results combine the query results of multiple select statements together.
【Related learning recommendations:mysql tutorial(Video)】
MySQL multi-table joint query statement is:
The joint query result is to combine the query results of multiple select statements together.
You can use the union and union all keywords to merge.
Basic syntax:
select statement 1
union [union option]
select statement 2
union [union Options]
select statement n
The union option has two options: all (indicates duplicates are also output); distinct (removes duplicates, completely duplicates, will be removed by default) Heavy)
The fields of the two tables can be consistent.
例: select id,addrid from addr union all select id,addrid from student
The meaning of joint query
1. Query the same table, but with different requirements
2.Multiple table query: the structure of multiple tables Exactly the same, the saved data (structure) is also the same
Use of order by in joint query
In joint query: order by can only be used at the end, and you need to use parentheses for the query statement. OK.
例: ---(错误) select * from student where sex="man" order by score union select * from student wherre sex="woman" order by score; 这种情况会报错,因为一个句子中不能有两个order by ---(正确但不符合所需) select * from student where sex="man" union select * from student wherre sex="woman" order by score; 这种情况是正确的,但是合并又没有意义,他会把之前的sex分好的情况给打乱 ---(正确) (select * from student where sex="man" order by score limit 10) union (select * from student wherre sex="woman" order by score limit 10); 在子语句中使用order by,由于优先级的问题,需要将整个子句用()括起来,且必须和limit结合使用,否则不会生效。
If you want to learn more about programming, please pay attention to thephp trainingcolumn!
The above is the detailed content of What is the multi-table joint query statement in mysql. For more information, please follow other related articles on the PHP Chinese website!