sql - 比较 Where 子句中两列是否相等

标签 sql database

我想比较 where 子句中的两列是否相等。列是两列来自已连接的不同表

这是我正在使用的语句

SELECT authors.state, sum(qty*price)as sales_revenue 
  from authors,titles, stores, sales 
 WHERE authors.state = (SELECT  stores.state from stores)

但我收到一个错误提示 中号

sg 8120, Level 16, State 1, Line 1
Column 'authors.state' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

最佳答案

使用这个:

WHERE authors.state = stores.state

或者更好的是,使用 ANSI-92 JOIN 语法。

SELECT authors.state, sum(qty*price) as sales_revenue
FROM authors
JOIN stores ON authors.state = stores.state
JOIN titles ON ...
JOIN sales ON ...
GROUP BY authors.state

关于sql - 比较 Where 子句中两列是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5467660/

相关文章:

mysql - 使用连接检索空值时的困惑

sql - 希望使用 SQL 中的索引查找大型数据集中的重复项

sql - 获取缺少答案的调查问卷列表

php - Post 函数无法按预期与数据库一起运行

database - 将 n-gram 存储在数据库中 < n 个表中

mysql - 如何在特定数据库中创建表?

测验的 SQL 表结构和关系

sql - 寻找下一个空闲时间段

php - 选择日期之前

database - 事实表和维度表的区别?