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

时间:2011-03-29 03:42:15

标签: 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.

2 个答案:

答案 0 :(得分:4)

使用此:

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

答案 1 :(得分:0)

此:

SELECT a.state,
       sum(l.qty*l.price)as sales_revenue 
       FROM authors a
       LEFT JOIN stores s on a.state=s.state
       GROUP BY a.state

指定每个表中可用的列,以便检查其关系

例如

authors = id,state
store = id,store

此致