简体   繁体   中英

How I can do this query in MySQL (multiple columns from different tables)

I want to select 3 columns from different tables but I must to use functions, this is the query that I want to do:

SELECT DISTINCT table1.sum(column1) as sumcol1,
      table2.sum(column2) as sumcol2, table3.column3 as col3 
 FROM db1.table1, db1.table2, db1.table3 
WHERE table1.id = 1  AND table2.id = 1 AND table3.id = 1;

note: only table3.id is unique index
and I want to get this table:

 |sumcol1|sumcol2|col3|
 |1234556|5432113|432|

Since you did not mentioned the logic. Looks like missplaced SUM

SELECT DISTINCT sum(table1.column1) as sumcol1,
          sum(table2.column2) as sumcol2, table3.column3 as col3 
     FROM db1.table1, db1.table2, db1.table3 
    WHERE table1.id = 1  AND table2.id = 1 AND table3.id = 1;

I found the solution, it was simple.

SELECT DISTINCT sum(table1.column1) as sumcol1, sum(table2.column2) as sumcol2, table3.column3 as col3 FROM db1.table1, db1.table2, db1.table3 WHERE table1.id = 1 AND table2.id = 1 AND table3.id = 1;

I just change table1.sum(column1) for sum(table1.column1) and i change table2.sum(column2) for sum(table2.column2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM