| | |
multiselecting UNLIKE values
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2008
Posts: 31
Reputation:
Solved Threads: 0
hi. my thread title seems strange but its really about comparing unlike values as an output.
I want all values that are not LIKE the other values. To make things clearer. Here's my query:
What I want to do in here is select all columns that has the column name value,which is not equivalent to the other column from another table. So its like saying, "From all eggs that came from Hens , select those who are not eggs of Hen # 1."
When I tried the NOT LIKE query. it still showed the ones that are LIKE and worse, it redundantly displayed its data. Any suggestions?
I want all values that are not LIKE the other values. To make things clearer. Here's my query:
SELECT Records.[Officer ID], Records.[Plate Number], Records.[Violation Commited], Violations.[Violation Code], Records.[Street Name], Records.[City of Apprehension], Records.[Vehicle Category], Records.[Vehicle Type], Records.[Vehicle Brand], Records.[Date/Time Apprehended] FROM Records,Violations,DriverInfo WHERE Records.[Plate Number] NOT LIKE DriverInfo.[Plate Number] and Records.[Violation Commited] like Violations.Violations
What I want to do in here is select all columns that has the column name value,which is not equivalent to the other column from another table. So its like saying, "From all eggs that came from Hens , select those who are not eggs of Hen # 1."
When I tried the NOT LIKE query. it still showed the ones that are LIKE and worse, it redundantly displayed its data. Any suggestions?
Try using != (not equal) instead of NOT LIKE. LIKE is a little more 'fuzzy' than an explicit equal, which is why you did not get the data you were looking for.
Also, one other thing that might help make your queries easier to read (and type) is using aliases for your tables:
I hope this helps.
Also, one other thing that might help make your queries easier to read (and type) is using aliases for your tables:
SQL Syntax (Toggle Plain Text)
SELECT r.[Officer ID], r.[Plate Number], r.[Violation Commited], v.[Violation Code], r.[Street Name], r.[City of Apprehension], r.[Vehicle Category], r.[Vehicle Type], r.[Vehicle Brand], r.[DATE/TIME Apprehended] FROM Records r, Violations v, DriverInfo di WHERE r.[Plate Number] != di.[Plate Number] AND r.[Violation Commited] = v.Violations
I hope this helps.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
hello,
You want to select some data from three tables. This will be done by two inner joins. If you don't tell which columns of the involved tables should be joined together the result will be cartesian product (cross join), that is all columns of 1st table will be joined with all columns of 2nd table and all columns of that result will be joined with all columns of 3rd table what usually is an undesirable result. Similar results will be given by LIKE and NOT LIKE comparison operators what might be theoretically correct operators for joining constraints but rather unlikely.
If your select statement contains columns from various tables you must:
1. Figure out the correct joining constraints for WHERE or the newer ON clause
2. Supply further predicates for the WHERE clause to reduce result set to the
rows you really expect.
Instead of doing joins via where clause there is a more modern joining syntax:
As cmhampton allready advised aliases make life easier:
Instead of the very common = operator {<, <=, =>, >, !=} are very rarely used (to do
such called theta join). In your case you only need = operator (to do such called equi join).
You may check these joining constraints first to see whether redundant rows disappear. Then add further predicates to where clause to solving: "From all eggs that came from Hens , select those who are not eggs of Hen # 1." (This can never be get by join constraints only.)
krs,
tesu
You want to select some data from three tables. This will be done by two inner joins. If you don't tell which columns of the involved tables should be joined together the result will be cartesian product (cross join), that is all columns of 1st table will be joined with all columns of 2nd table and all columns of that result will be joined with all columns of 3rd table what usually is an undesirable result. Similar results will be given by LIKE and NOT LIKE comparison operators what might be theoretically correct operators for joining constraints but rather unlikely.
If your select statement contains columns from various tables you must:
1. Figure out the correct joining constraints for WHERE or the newer ON clause
2. Supply further predicates for the WHERE clause to reduce result set to the
rows you really expect.
MS SQL Syntax (Toggle Plain Text)
SELECT Records.[Officer ID], Records.[Plate Number], Records.[Violation Commited], Violations.[Violation Code], Records.[Street Name], Records.[City of Apprehension], Records.[Vehicle Category], Records.[Vehicle Type], Records.[Vehicle Brand], Records.[Date/Time Apprehended] FROM Records,Violations,DriverInfo WHERE -- two constraints for inner joins Records.[Plate Number] = DriverInfo.[Plate Number] AND Records.[Violation Commited] = Violations.Violations AND -- your specific predicates following here
MS SQL Syntax (Toggle Plain Text)
SELECT Records.[Officer ID], Records.[Plate Number], Records.[Violation Commited], Violations.[Violation Code], Records.[Street Name], Records.[City of Apprehension], Records.[Vehicle Category], Records.[Vehicle Type], Records.[Vehicle Brand], Records.[Date/Time Apprehended] FROM Records INNER JOIN DriverInfo ON Records.[Plate Number] = DriverInfo.[Plate Number] INNER JOIN Violations ON Records.[Violation Commited] = Violations.Violations WHERE -- your specific predicates here
MS SQL Syntax (Toggle Plain Text)
... FROM Records r INNER JOIN DriverInfo d ON r.[Plate Number] = d.[Plate Number] INNER JOIN Violations v ON r.[Violation Commited] = v.Violations WHERE -- your specific predicates here
such called theta join). In your case you only need = operator (to do such called equi join).
You may check these joining constraints first to see whether redundant rows disappear. Then add further predicates to where clause to solving: "From all eggs that came from Hens , select those who are not eggs of Hen # 1." (This can never be get by join constraints only.)
krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
sql Syntax (Toggle Plain Text)
SELECT productId, productName, productDescription, cost, ammount, minimumAmount FROM dbo.Products WHERE (productId <> '123')
Last edited by peter_budo; Mar 21st, 2009 at 3:15 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
![]() |
Other Threads in the MS SQL Forum
- Previous Thread: Best way to deploy MSSQL program?
- Next Thread: dataAdapter.Update problem
Views: 1653 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday





