logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
SQL Problems

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related
View previous topic :: View next topic  
Author Message
Primogenitor



Joined: 08 Jan 2005
Posts: 88

PostPosted: Thu Feb 03, 2005 9:57    Post subject: SQL Problems Reply with quote

Im sure this is but the first of many SQL problems I will have, but I shall plod on regardless. Thanks in advance for any help you can offer!

What is wrong with these statements, and how can I fix it?
Code:

o Got request: CREATE TABLE grid (areaid varchar(255),xaxis varchar(255),yaxis varchar(255),zaxis varchar(255),aaxis varchar(255),baxis varchar(255),caxis varchar(255))
..snip...
o Got request: SELECT areaid FROM grid WHERE xaxis = '500' AND yaxis = '500' AND zaxis = '500' AND aaxis = '500' AND baxis = '500' AND caxis = '500
! SQL Error: [Microsoft][ODBC Microsoft Access Driver] Syntax error in string in query expression 'xaxis = '500' AND yaxis = '500' AND zaxis = '500' AND aaxis = '500' AND baxis = '500' AND caxis = '500'.
o Sent response (-1 bytes):

Ive tried it without the ' (no luck) and Ive tried putting " instead (using GetName on an object named " ), but it doesnt seem to show in the log file.
Though this is with Access, I need the code to work mainly with SQLite. I just havnt got around to changing the database ono my home PC yet.
Back to top
View user's profile Send private message
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Thu Feb 03, 2005 12:45    Post subject: Reply with quote

Hmm, maybe try parentheses around the tests, e.g.

(yaxis = '500') and (...) and (...)
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
monezz



Joined: 03 Jan 2005
Posts: 52
Location: The Netherlands

PostPosted: Thu Feb 03, 2005 14:17    Post subject: Reply with quote

I think youre missing a ' after the last 500.
Back to top
View user's profile Send private message Visit poster's website
NoMercy



Joined: 03 Jan 2005
Posts: 123
Location: UK

PostPosted: Thu Feb 03, 2005 15:40    Post subject: Reply with quote

SQL could really use some nicer error messages :)
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Xildjian



Joined: 08 Jan 2005
Posts: 100

PostPosted: Thu Feb 03, 2005 16:02    Post subject: Reply with quote

You could also try putting brackets around your keys eg [key] =

I had to do this in my scripts. For some reason I have never understood I had to do this when quering a table I created with MS access. I never needed to do this with a table created in game, so I just put [] around all my keys, and things work great. No more syntax errors.
_________________
Member Shadow of Iniquity development team
Back to top
View user's profile Send private message
Primogenitor



Joined: 08 Jan 2005
Posts: 88

PostPosted: Fri Feb 11, 2005 17:05    Post subject: Reply with quote

Papillon wins the first correct answer prize, ( ) around them fixes it nicely, so it ends up looking like:
Code:


SELECT areaid FROM grid WHERE ( xaxis = '500' ) AND ( yaxis = '500' ) AND ( zaxis = '500' ) AND ( aaxis = '500' ) AND ( baxis = '500' ) AND ( caxis = '500' )


But now I have a different problem. How do I select rows whose value matches a value of a row in a different table?

e.g. I have two tables, Table_A and Table_B. Table_A is
A1 A2
1 3
2 4
3 4
4 5
Table_B is:
B1 B2
3 4
4 4
5 3

I want all the data in A1 where the B2 entry on the same row as the matching A2 to B1 is equal to 4. I.e. I want 1,2,3 in my result set but not 4.

Can I do layered SELECT statements? e.g.
SELECT A1 FROM Table_A WHERE A2 = (SELECT B1 FROM Table_B WHERE B2 = 4)
If not, what can I do instead?
Back to top
View user's profile Send private message
monezz



Joined: 03 Jan 2005
Posts: 52
Location: The Netherlands

PostPosted: Fri Feb 11, 2005 17:34    Post subject: Reply with quote

select A1
from table_a
, table_b
where A2 = B1
and B2 = 4

or

SELECT A1 FROM Table_A WHERE A2 in (SELECT B1 FROM Table_B WHERE B2 = 4)

Both would produce the same result.
The first query is generally faster becase the second one uses an inline view.

The example query would give you error (singe-row subquery returns more then one row in oracle), the in keyword solves that problem.

Gr.Monezz
Back to top
View user's profile Send private message Visit poster's website
Primogenitor



Joined: 08 Jan 2005
Posts: 88

PostPosted: Wed Feb 16, 2005 12:54    Post subject: Reply with quote

Thank you, that did it nicely.

Now what about if I want to check more than one row in the second table. For example:

I have two tables, Table_A and Table_B. Table_A is
A1 A2 A3
1 3 2
2 4 2
3 4 4
4 5 3
Table_B is:
B1 B2
2 3
3 4
4 4
5 3

I want all the data in A1 where the B2 entry on the same row as the matching A2 to B1 is equal to 4 and where the B2 entry on the same row as the matching A3 to B1 is equal to 4 I.e. I should get only 3 in my data set.

Can I do:

select A1
from table_a , table_b
where A2 = B1
and A3 = B1
and B2 = 4
Back to top
View user's profile Send private message
monezz



Joined: 03 Jan 2005
Posts: 52
Location: The Netherlands

PostPosted: Wed Feb 16, 2005 15:20    Post subject: Reply with quote

That query would produce 1 record.
You will ony get A1 = 3.
If thats what you want thenthe query is correct.

I would rewrite the where clause to:
where A2 = A3
and A2 = B1
and B2 = 4

but thats just a matter of taste. Both work the same.

I would recomend to use aliases for the tables.
It will make you're query easier to read with more functional names

example:

Select pla.id
, pla.ip_address
, cha.name
from players as pla
, characters as cha
where cha.pla_id = pla.id
and cha.class_1 = "Fighter"
and cha.xp <= 3000
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group