0

我正在使用一个包含 50 个列(带有位值)的表,每个 50 个美国州。我试图找到一个 SQL 语句,它将返回所有包含 1 的列。我四处寻找没有运气的方法。

任何帮助将非常感激

4

2 回答 2

3

If this is a new development, and not for existing tables I would suggest the following. (Otherwise I'm stumped.) It sounds like you have implemented a many-to-many. I would suggest that you implement a table for States as suggested by @juergen d (albeit without the bit field); implement your current entity as-is but without the columns for states. And then implement a third table that has two columns, one for a state's ID and another for the ID of your entity.

Then instead of setting bit fields on your entity's table, you would create entries in this third table.

You can then perform joins on tables to obtain the states set on a certain entity.

For more info see http://en.wikipedia.org/wiki/Many-to-many_(data_model) and http://www.lornajane.net/posts/2011/inner-vs-outer-joins-on-a-many-to-many-relationship

于 2013-10-15T21:18:47.767 回答
3

你最好改变你的桌子设计。最好使用这样的东西

States table
-------------------
id       int
name     varchar(100)
bitcol   bit(1)

然后你可以选择这样的状态

select name from States
where bitcol = 1
于 2013-10-15T21:06:52.543 回答