views:

102

answers:

1

Hi, I can not get the difference betwwn these statements? would you please help me,I have read some sample of select statements but I did not get these ones.

SELECT 'B' FROM T WHERE A = (SELECT NULL);
SELECT 'C' FROM T WHERE A = ANY (SELECT NULL);    
SELECT 'D' FROM T WHERE A = A;

I use MySQL

EDITED: also it has a conditional part that : SET ANSI_NULLS is OFF

+3  A: 

ANSI_NULL ON

SELECT 'B' FROM T WHERE A = (SELECT NULL);

is the same as:

SELECT 'B' FROM T WHERE A = NULL;

Which always returns an empty set because anything compared to NULL returns NULL, not TRUE. You might try this:

SELECT 'B' FROM T WHERE A IS NULL;

The second query is basically the same and will return an empty set:

SELECT 'C' FROM T WHERE A = ANY (SELECT NULL);

If A has a value, the following will return all rows:

SELECT 'D' FROM T WHERE A = A;

Except, if A IS NULL, it will return an empty set.

ANSI_NULL OFF

SELECT 'B' FROM T WHERE A = (SELECT NULL);

is the same as:

SELECT 'B' FROM T WHERE A = NULL;

Which returns all rows where A IS NULL.

The second query is basically the same and will return rows where A IS NULL:

SELECT 'C' FROM T WHERE A = ANY (SELECT NULL);

The following will always return all rows, regardless if A IS NULL:

SELECT 'D' FROM T WHERE A = A;
Marcus Adams
exceuse me I have asked a question before likeSELECT 'A' FROM T WHERE A = NULL; and the others told me that it will return some rows which contains A character!!!
http://stackoverflow.com/questions/2745849/mysql-column-names-and-aliases
@user329820 the above post is correct. To compare with NULL, you must use IS. Try it out yourself!
Blorgbeard
Also, I think most people answering that other question mentioned that you must change `=` to `IS`.
Blorgbeard
also I found this site ,please see it ,thanks http://msdn.microsoft.com/en-us/library/ms188048.aspx
if the column A was set with null value then the result of first statement in the above will be false?(ANSI-NULLS is OFF)
@user329820, I edited my answer for ANSI_NULL ON and OFF.
Marcus Adams