I recently ran into a very odd MySQL behavior, which is either a glaring bug or an example of our old friend "undefined behavior"—I can't find or invent any reason it should not be defined in a more reasonable way, however, so ... well, somebody here will know the answer, I'm sure, so I just hope he or she feels like telling me. :-)

The behavior can be summarized by the following set of results:

mysql> select count(1) from foo where remote in (select remote from ba +r); +----------+ | count(1) | +----------+ | 5750 | +----------+ 1 row in set (0.30 sec) mysql> select count(1) from foo ; +----------+ | count(1) | +----------+ | 94587 | +----------+ 1 row in set (0.01 sec) mysql> select count(1) from foo where remote not in (select remote fro +m bar); +----------+ | count(1) | +----------+ | 56 | +----------+ 1 row in set (0.45 sec) mysql> select count(1) from votes where remote is null; +----------+ | count(1) | +----------+ | 112 | +----------+ 1 row in set (0.02 sec)

As you can see, the query which should be returning 88837 rows (roughly) is returning 56, which is, probably not coincidentally, one half the number of rows in the table for which the "remote" column is null. (Never mind why it's null—it was a historical artifact.) When the NULL values are set to an empty string, the problem goes away.

Clearly, returning the first (#nulls)/2 rows of the table (I *believe* that was what was getting returned) is not the desired behavior for this type of query—my best interpretation is that the NULL rows should not have been included in either of the two sets—so the question is whether the query should ever have been allowed in the first place on a column that can contain null values. I believe that it should, and hence that this behavior (in mysql Ver 14.7 Distrib 4.1.10, for portbld-freebsd4.10 (i386)) is a bug. Anybody want to explain why I'm horribly wrong?



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders


In reply to (OT) MySQL bug? Or PEBKAC? by ChemBoy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.