Home  >  Q&A  >  body text

Arrangement problem after mysql query

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条数据,我想把查询的结果放在一条 
select a.a,a.b,b.a,b.b from table1 a,table2 b where a.id = b.aid
这种是可以 然后b表有多条数据和a关联的时候的时候我想重命名字段名
我想要的结果:
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
现在我不确定table2有几条数据是和table1绑定的,而且table2数据查询出来的字段如果有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

伊谢尔伦伊谢尔伦2670 days ago738

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-28 09:25:42

    select * from table1 as a right join table2 as b on a.id = b.aid;

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-28 09:25:42

    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!

    reply
    0
  • 高洛峰

    高洛峰2017-06-28 09:25:42

    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?

    reply
    0
  • Cancelreply