943,701 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 3759
  • MS SQL RSS
Jul 11th, 2008
0

multiselecting UNLIKE values

Expand Post »
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:

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
harcaype is offline Offline
31 posts
since May 2008
Jul 11th, 2008
0

Re: multiselecting UNLIKE values

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:

SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. r.[Officer ID],
  3. r.[Plate Number],
  4. r.[Violation Commited],
  5. v.[Violation Code],
  6. r.[Street Name],
  7. r.[City of Apprehension],
  8. r.[Vehicle Category],
  9. r.[Vehicle Type],
  10. r.[Vehicle Brand],
  11. r.[DATE/TIME Apprehended]
  12. FROM
  13. Records r,
  14. Violations v,
  15. DriverInfo di
  16. WHERE
  17. r.[Plate Number] != di.[Plate Number]
  18. AND r.[Violation Commited] = v.Violations

I hope this helps.
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Jul 11th, 2008
0

Re: multiselecting UNLIKE values

Thanks! But I have also used != and the result is still the same
Reputation Points: 10
Solved Threads: 0
Light Poster
harcaype is offline Offline
31 posts
since May 2008
Jul 11th, 2008
0

Re: multiselecting UNLIKE values

Click to Expand / Collapse  Quote originally posted by harcaype ...
Thanks! But I have also used != and the result is still the same
Can you make up some sample data for me? I'll recreate this structure on my server and test it.
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Jul 11th, 2008
0

Re: multiselecting UNLIKE values

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.

MS SQL Syntax (Toggle Plain Text)
  1. SELECT Records.[Officer ID],
  2. Records.[Plate Number],
  3. Records.[Violation Commited],
  4. Violations.[Violation Code],
  5. Records.[Street Name],
  6. Records.[City of Apprehension],
  7. Records.[Vehicle Category],
  8. Records.[Vehicle Type],
  9. Records.[Vehicle Brand],
  10. Records.[Date/Time Apprehended]
  11. FROM Records,Violations,DriverInfo
  12. WHERE
  13. -- two constraints for inner joins
  14. Records.[Plate Number] = DriverInfo.[Plate Number] AND
  15. Records.[Violation Commited] = Violations.Violations
  16. AND
  17. -- your specific predicates following here
Instead of doing joins via where clause there is a more modern joining syntax:
MS SQL Syntax (Toggle Plain Text)
  1. SELECT Records.[Officer ID],
  2. Records.[Plate Number],
  3. Records.[Violation Commited],
  4. Violations.[Violation Code],
  5. Records.[Street Name],
  6. Records.[City of Apprehension],
  7. Records.[Vehicle Category],
  8. Records.[Vehicle Type],
  9. Records.[Vehicle Brand],
  10. Records.[Date/Time Apprehended]
  11. FROM
  12. Records INNER JOIN DriverInfo ON Records.[Plate Number] = DriverInfo.[Plate Number]
  13. INNER JOIN Violations ON Records.[Violation Commited] = Violations.Violations
  14. WHERE
  15. -- your specific predicates here
As cmhampton allready advised aliases make life easier:
MS SQL Syntax (Toggle Plain Text)
  1. ...
  2. FROM
  3. Records r INNER JOIN DriverInfo d ON r.[Plate Number] = d.[Plate Number]
  4. INNER JOIN Violations v ON r.[Violation Commited] = v.Violations
  5. WHERE
  6. -- your specific predicates here
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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Mar 21st, 2009
0

try this one

sql Syntax (Toggle Plain Text)
  1. SELECT productId, productName, productDescription, cost, ammount, minimumAmount
  2. FROM dbo.Products
  3. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
edgarlip is offline Offline
1 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Best way to deploy MSSQL program?
Next Thread in MS SQL Forum Timeline: dataAdapter.Update problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC