Question: I get different results when I use a subquery or a JOIN.

The query are these:

```sql
SELECT
    COUNT(*)
FROM
    table2
WHERE
    table2.id NOT IN (
        SELECT fkfield FROM table1
    )

```

and

```sql
SELECT
    COUNT(*)
FROM
    table2
LEFT JOIN table1 ON table1.fkfield = table2.id
WHERE
    table1.id IS NULL

```

(`table1.fkfield` has a FK constraint to `table2.id`)

First query returns 0 rows, second query returns 396 rows.

I've tried `OPTIMIZE TABLE` and export/import (with mysqldump) with no luck.
The engine is **Innodb**.

Thanks in advance.

Answer: 
Well to answer your questions and clarify things here:

> 
> I thought `g()` should return `1` but it was `2`. 
> 
> 
> 

No `g()` doesn't return anything, it don't have a `return` statement, so it won't return `1` nor `2`.

> 
> And why `x` return `undefined` after I invoked `g()`?
> 
> 
> 

And `x` is local to `f` function, so it will throw `Uncaught ReferenceError: x is not defined`, if you call it from outside `f` function.

**EDIT:**

After your Edit you asked:

> 
> I thought `f()` should return `1` but it was `2`. 
> 
> 
> 

Well in that case I would say that this is the right behaviour and the expected result from calling `f()`, it should return `2` because `x` is local to `f()` function and `g()` can access this local variable as it's local to `f()` too, so when you intialize `x` to `1` in `f` and then call `g()` before returning `x`, it will set `x` to `2` that's why you get `2`.
As you can see :

```
g = function () {
         x = 2;
};

```

Doesn't have a return statement ~~AND is declared within `f`'s scope (therefore not accessible through the global scope)~~ ; therefore it'll not return any value (hints the undefined).

For `f` :

```
 var f = function () {
     var x = 1;
     g = function () {
         x = 2;
     };
     g();
     return x;
 };

```

When you call `f` it declares a variable `x` and initializes it to 1.
Then you declare an "inner function" that modifies `x` (since `x` has been declared prior to `g` it can be accessed through `g`'s scope. After `g`'s declaration you call it, therefore x becomes 2. Therefore `f`'s return value is 2.