View previous topic :: View next topic |
Author |
Message |
Primogenitor
Joined: 08 Jan 2005 Posts: 88
|
Posted: Thu Feb 03, 2005 9:57 Post subject: SQL Problems |
|
|
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 |
|
|
Papillon x-man
Joined: 28 Dec 2004 Posts: 1060 Location: Germany
|
Posted: Thu Feb 03, 2005 12:45 Post subject: |
|
|
Hmm, maybe try parentheses around the tests, e.g.
(yaxis = '500') and (...) and (...) _________________ Papillon |
|
Back to top |
|
|
monezz
Joined: 03 Jan 2005 Posts: 52 Location: The Netherlands
|
Posted: Thu Feb 03, 2005 14:17 Post subject: |
|
|
I think youre missing a ' after the last 500. |
|
Back to top |
|
|
NoMercy
Joined: 03 Jan 2005 Posts: 123 Location: UK
|
Posted: Thu Feb 03, 2005 15:40 Post subject: |
|
|
SQL could really use some nicer error messages :) |
|
Back to top |
|
|
Xildjian
Joined: 08 Jan 2005 Posts: 100
|
Posted: Thu Feb 03, 2005 16:02 Post subject: |
|
|
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 |
|
|
Primogenitor
Joined: 08 Jan 2005 Posts: 88
|
Posted: Fri Feb 11, 2005 17:05 Post subject: |
|
|
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 |
|
|
monezz
Joined: 03 Jan 2005 Posts: 52 Location: The Netherlands
|
Posted: Fri Feb 11, 2005 17:34 Post subject: |
|
|
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 |
|
|
Primogenitor
Joined: 08 Jan 2005 Posts: 88
|
Posted: Wed Feb 16, 2005 12:54 Post subject: |
|
|
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 |
|
|
monezz
Joined: 03 Jan 2005 Posts: 52 Location: The Netherlands
|
Posted: Wed Feb 16, 2005 15:20 Post subject: |
|
|
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 |
|
|
|