简体   繁体   English

我如何在MySQL中执行此查询(来自不同表的多个列)

[英]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: 我想从不同的表中选择3列,但必须使用函数,这是我要执行的查询:

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 注意:只有table3.id是唯一索引
and I want to get this table: 我想要这张桌子:

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

Since you did not mentioned the logic. 由于您没有提到逻辑。 Looks like missplaced SUM 看起来像放错了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; 从db1.table1,db1.table2,db1.table3中选择DISTINCT sum(table1.column1)作为sumcol1,sum(table2.column2)作为sumcol2,table3.column3作为col3在哪里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) 我只是将table1.sum(column1)更改为sum(table1.column1),然后将table2.sum(column2)更改为sum(table2.column2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 mysql 中的 2 个不同表进行多次查询? - How to do multiple while query from 2 different tables in mysql? 如何在 SQL 中显示 3 个不同表中的多列? - How do I display multiple columns from 3 Different tables in SQL? MySQL:我如何从多个表中获取3列? - Mysql : how i can get 3 columns from multiple tables? 如何在一个MySQL查询中从不同的表中进行选择? - How can I select from different tables in one MySQL query? 如何查询2个表和多个列? - How Can I Query 2 Tables And Multiple Columns? 如何编写 mysql 查询以从一个表中获取记录列表,其中列与多个其他表连接 - How do I write a mysql query to get a list of records from one table with columns concatenated from multiple other tables 我如何编写一个 SQL 查询来从多个表中进行全文搜索,然后加入来自不同表的结果? - How can I write a SQL query to do a full text search from multiple tables and then join the results comming from different tables? MYSQL 查询求和不同表中的多列 - MYSQL query to sum multiple columns in different tables MySQL GROUP BY来自不同表的多个列 - MySQL GROUP BY multiple columns from different tables 在PHP中,如何使用跨不同表的多个“ IN”语句查询mySQL数据库? - In PHP, how do I query a mySQL database using multiple “IN” statements across different tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM