问题
$consulta3 = "SELECT * FROM Dept INNER JOIN Userinfo INNER JOIN Checkinout
on Dept.DeptName = '$departamento'
where Dept.Deptid = Userinfo.Deptid AND Userinfo.Name = Checkinout.name";
the thing im trying to do with this query is: i have a table called Dept where it has all the deparments names and ids, User info that has user name, a department id and a id. Also i have a 3rd table called checkinout that has user id, with a time.
So i want (using the deparment's name) to bring all the data from the Checkinout table from the people that belongs to that deparment, but when i try that query it gives me an error:
syntax error in from clause. sql state 37000 in sqlexecdirect
and i cant solve it. Im using a Acces Data base.
Thank you very much for you help.
回答1:
Access absolutely requires parentheses in the FROM
clause of any query which includes more that one join. If you have Access available, create and test a new query in the query designer. One of the reasons the designer is useful is that it knows the rules for parentheses which keep the db engine happy.
Start with a query similar to this. Don't worry about filtering based on Dept.DeptName
at this point. Just make sure the joins are set up correctly.
SELECT *
FROM
(Dept
INNER JOIN Userinfo
ON Dept.Deptid = Userinfo.Deptid)
INNER JOIN Checkinout
ON Userinfo.Name = Checkinout.name
After you have the joins set up correctly, add in your filter constraint (WHERE Dept.DeptName ...
).
回答2:
You have reversed the inner join and where clauses. You should write the SQL like this:
SELECT *
FROM Dept
INNER JOIN Userinfo ON Dept.Deptid = Userinfo.Deptid
INNER JOIN Checkinout ON Userinfo.Name = Checkinout.name"
WHERE Dept.DeptName = '$departamento'
回答3:
Probably should be:
SELECT * FROM Dept
INNER JOIN Userinfo ON Dept.Deptid = Userinfo.Deptid
INNER JOIN Checkinout on Userinfo.Name = Checkinout.name
where Dept.DeptName = '$departamento'
回答4:
I think it should be :
SELECT * FROM (Dept
INNER JOIN Userinfo ON (Dept.Deptid = Userinfo.Deptid))
INNER JOIN Checkinout ON (Userinfo.Name = Checkinout.name)
WHERE Dept.DeptName = '$departamento'
回答5:
what i decided to do is to use 2 different queries:
$consulta= "SELECT * FROM Dept INNER JOIN Userinfo
ON Userinfo.Deptid = Dept.Deptid
where Dept.DeptName = '$departamento'";
and
$consulta2 = "SELECT * FROM Checkinout, Userinfo
where Checkinout.Userid = '$userid' AND Userinfo.userid = '$userid' AND
Checkinout.Checktime BETWEEN CDate('$fecha1') AND CDate('$fecha2')";
The first one to select all the people that belong to one deparment, and the second one to find all the INs and OUTs of one person. Both in a while to be able to look each register of the first result and check with the other query
still have problems with the search, its not working fine, im trying to do a full query where it searchs in the 3 tables at the same time but its giving me a error:
$toda_consulta= "SELECT *
FROM Dept
INNER JOIN Userinfo ON (Dept.Deptid = Userinfo.Deptid)
INNER JOIN Checkinout ON (Checkinout.Userid = Userinfo.Userid)
WHERE Dept.DeptName = '$departamento' AND
Checkinout.Checktime BETWEEN CDate('$fecha_inicio') AND CDate('$fecha_fin')";
error: Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][Controlador ODBC Microsoft Access] sintax error (missing operator) in the expression '(Dept.Deptid = Userinfo.Deptid) INNER JOIN Checkinout ON (Checkinout.Userid = Userinfo.Userid)'., SQL state 37000 in SQLExecDirect in C:\wamp\www\casa 7-9-13\calcula_departamento1.php on line 51
than you for your help
来源:https://stackoverflow.com/questions/18967627/access-query-error-syntax-error-in-from-clause