There are two 2 tables, table1 and table2
. The problem is as follows:
select a.a,a.b from table1 a;
select b.a,b.b from table2 b;
有2個查詢,比如table1 和 table2 都只有1條數(shù)據(jù),我想把查詢的結(jié)果放在一條
select a.a,a.b,b.a,b.b from table1 a,table2 b where a.id = b.aid
這種是可以 然后b表有多條數(shù)據(jù)和a關聯(lián)的時候的時候我想重命名字段名
我想要的結(jié)果:
select a.a,a.b,b.a,b.b,c.a,c.b from table1 a,table2 b,table2 c where a.id = b.aid and a.id=c.aid
現(xiàn)在我不確定table2有幾條數(shù)據(jù)是和table1綁定的,而且table2數(shù)據(jù)查詢出來的字段如果有5條每條字段名稱都需要重命名
求個解決方法
Maybe my description is not clear. If the table query is possible, I don’t need to ask questions.
There are now 3 tables: A: id, caseid B: id, caseid, accidentid, name (Zhang San) C: id, caseid, accident, name (Li Si)
B and C are the same table, but the data is different, but they are bound to this A
The final format I want to query is: A.id,A .caseid,B.accident,B.name,C.accident,C.name This is a piece of data with 6 columns
小伙看你根骨奇佳,潛力無限,來學PHP伐。
select * from table1 as a right join table2 as b on a.id = b.aid;
select * from table1 as a right join table2 as b on a.id = b.aid;
Usage of right join
This statement means to use table2 as the main table to connect table1
And the name of the field you take out is a.a, etc. This will not be repeated, because your field specifies which table this field comes from.
If you want to rename it, you can use a.a as T1-a (any name)
select * from table2 as b left join table1 as a on a.id = b.aid;
You can also use left join to just change the positions of table1 and table2!
Question 1: To return different numbers of fields, should they be placed in a SQL statement?
Question 2: Can table2 only have a maximum number of different fields?