Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
316hitmanhart
Starting Member
2 Posts |
Posted - 2010-10-13 : 13:13:48
|
| HiI'm new to SQL and was wondering if anyone can help me.I have a table with four columns called a,b,c and d.In column a I have entered the values 1,5,9 and 13In column b I have entered the values 2,6,10 and 14In column c I have entered the values 3,7,11 and 15In column d I have entered the values 4,8,12 and 16I want to be able to run a query that can search all four columns for one value.Currently I have the following query which works:-select * from Table_1 where a = 7OR b = 7OR c = 7OR d = 7However I only want to have to enter the value once.I think the query should be something like this below it doesn't work.select * from Table_1 where a,b,c,d = 7Thanks for any help |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2010-10-13 : 13:19:32
|
Generally speaking if you want/need to search multiple columns for the same value that is an indication that your table is not designed properly. It sounds de-normalized. They should all be in the same column in different rows. Perhaps with some other column attribute to indicate what type of value it is.Otherwise, one way or another, your criteria will need to explicitly reference each column individually for a given value.EDIT:here is an example of what I mean:id sourceCol value1 a 12 a 53 a 94 a 135 b 26 b 67 b 108 b 149 c 310 c 711 c 1112 c 15select [id], sourceCol where [value] = 7 Be One with the OptimizerTG |
 |
|
|
316hitmanhart
Starting Member
2 Posts |
Posted - 2010-10-13 : 13:31:26
|
| The database is going to used my staff.I'll explain a bit furtherMy company is a key cutting company. There are several makes and models of keys each with there own ID no. This ID then corrosponds to an ID of the keys my company uses. So my data will look abit like thisMy Key ID No Key Ref 1 Key Ref 2 Key Ref 31 1234 5464 45622 9845 7777 4563 3 4563 2154 78954 0123 8465 9875The idea here being that if someone brings in a key to be cut and the ref ID on it is 9875 then my staff would know it Key ID No 4 from our stock.I want to be able to search all columns because my staff may not know which column 9875 is in |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2010-10-13 : 14:45:21
|
| NORMALIZATIONhttp://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2010-10-13 : 15:48:20
|
quote: Originally posted by 316hitmanhart The database is going to used my staff.I'll explain a bit furtherMy company is a key cutting company. There are several makes and models of keys each with there own ID no. This ID then corrosponds to an ID of the keys my company uses. So my data will look abit like thisMy Key ID No Key Ref 1 Key Ref 2 Key Ref 31 1234 5464 45622 9845 7777 4563 3 4563 2154 78954 0123 8465 9875The idea here being that if someone brings in a key to be cut and the ref ID on it is 9875 then my staff would know it Key ID No 4 from our stock.I want to be able to search all columns because my staff may not know which column 9875 is in
This added detail fits perfectly with my suggestion.Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|