views:

88

answers:

3
+1  Q: 

Stupid sql error.

Hello.

I have this sql statement:

SELECT game.game_id FROM game, userGame WHERE userGame.user_id = 1
AND userGame.game_id != game.game_id;

And I'm getting this:

1
1
2
2
3
3
4
4
4
5
5
5

I'm running the statement on a Sqlite3 database on Android 2.1.

The tables are:

USERGAME
--------
userId
gameId
more_columns

GAME
-----
gameId
more_columns

How can I solve this error?

UPDATE:
I want to get all game's id that they are not in usergame table when userId=1.

Thanks.

A: 

I'm not sure about the Sqlite syntax, but you can try something like this. Maybe someone else can straighten out the syntax?

SELECT  *
FROM    game a left outer join usergame b on a.gameId = b.gameId and b.userId = 1
WHERE   b.gameId IS NULL

OR

SELECT  *
FROM    game a, usergame b
WHERE   (a.gameId *= b.gameId AND b.userId = 1)
        AND b.gameId IS NULL

Basically, you're saying: "Give me all the games in the [game] table, but exclude the ones that userID 1 has."

I'm not sure if that's what you are trying to achieve.

XSaint32
Yes, that's what I'm trying to achieve.
VansFannel
`*=` is pre-ANSI-92 SQL Server specific OUTER JOIN syntax, no?
OMG Ponies
@OMG Ponies - It probably is. However, as I am unfamiliar with SQLite syntax, I provided only what I have known to work in T-SQL. I barely use *= myself.
XSaint32
@XSaint32: First sentence work on Sqlite, second doesn't work.
VansFannel
A: 
SELECT game_id 
  FROM game a  
 WHERE NOT EXISTS (Select 1 
                     from game b, userGame c 
                    where a.game_id=b.game_id and b.game_id=c.game_id and c.user_id=1)
Falcon
+1  A: 

Not exists syntax:

select gameID
from game g
where not exists
(select null from usergame ug 
 where g.gameID = ug.gameID and ug.userID = 1)
Mark Bannister
It works great on Sqlite. Thanks.
VansFannel