0

我有 20 列具有相同的数据。Dept_1,dept_2,dept_3 .... 我想查询数据库,其中每个列都匹配(伪代码:),而不必在子句where dept_1, dept_2, dept_3... = 'ACCOUNTING'中实际写出所有 20 个列。WHERE有没有办法做到这一点?

4

3 回答 3

2

我严重质疑数据库的设计......但也许这就是你要找的?

WHERE Dept_1 = 'ACCOUNTING'
  AND Dept_2 = 'ACCOUNTING'
  AND Dept_3 = 'ACCOUNTING'
  ... ad nauseum
  AND Dept_19 = 'ACCOUNTING'
  AND Dept_20 = 'ACCOUNTING'
于 2012-05-14T22:47:15.623 回答
0

I think it is easier to user the shorter version:

select *
from table
where 'ACCOUNTING' in (dept_1, dept_2, dept_3, . . . )

If you need for all the fields to be the same, then use a where clause with ANDs.

However, you might want to redesign the database to have a table with a separate row for each department.

于 2012-05-15T01:49:25.050 回答
0

最简单的方法可能是执行以下操作:

SELECT * FROM table
 WHERE dept_1 = 'ACCOUNTING'
   AND dept_2 = 'ACCOUNTING'
   AND dept_3 = 'ACCOUNTING'

或者

SELECT * FROM table
 WHERE dept_1 = 'ACCOUNTING'
    OR dept_2 = 'ACCOUNTING'
    OR dept_3 = 'ACCOUNTING'

这取决于您是要抓取所有 3 个字段都具有相同值的数据,还是要在至少 1 个字段匹配时抓取数据。

于 2012-05-14T22:52:40.630 回答