IN Query Expression . (Error 3075). MS ACESSS - sql

SQL Code: SELECT Alan1, Alan2, Alan3, Alan4, A.A_Ihbar_Sayısı, B.B_Ihbar_Sayısı, C.C_Ihbar_Sayısı FROM A INNER JOIN B ON A.Alan4 = B.Alan4 AND A.Alan3 = B.Alan3 AND A.Alan2 = B.Alan2 INNER JOIN C ON B.Alan4 = C.Alan4 AND B.Alan3 = C.Alan3 AND B.Alan2 = C.Alan2;
I get error in query expression . (Error 3075).I'm sure I didn't make a syntax error. How can I fix this situation?

MS Access has arcane syntax requirements, such as parentheses in the FROM clause for multiple JOINs:
SELECT Alan1, Alan2, Alan3, Alan4, A.A_Ihbar_Sayısı, B.B_Ihbar_Sayısı, C.C_Ihbar_Sayısı FROM (A INNER JOIN
B
ON A.Alan4 = B.Alan4 AND A.Alan3 = B.Alan3 AND A.Alan2 = B.Alan2
) INNER JOIN
C
ON B.Alan4 = C.Alan4 AND B.Alan3 = C.Alan3 AND B.Alan2 = C.Alan2;

Related

How can I solve this Query Error in Access?

I have the next error of syntax in Access
SELECT Maestros.Nombre AS Maestro, Materias.Nombre AS Materia
FROM Maestros
INNER JOIN Maestros_Materias ON Maestros.id = Maestros_Materias.Maestro_id
INNER JOIN Materias ON Materias.id = Maestros_Materias.Materia_id
WHERE Maestros.id = 1;
I don't know what is the error here. Thanks for your answers
In Access, you need nested parentheses to handle multiple JOINs.
This should do it:
SELECT Maestros.Nombre AS Maestro, Materias.Nombre AS Materia
FROM (Maestros
INNER JOIN Maestros_Materias ON Maestros.id = Maestros_Materias.Maestro_id)
INNER JOIN Materias ON Materias.id = Maestros_Materias.Materia_id
WHERE Maestros.id = 1;

UPDATE query with multiple INNER JOINs (syntax error)

I'm trying to run an update query with multiple join statements in VBA, but I keep getting "missing operator" errors. Same happens when I try to run the SQL in query builder. The SQL is:
UPDATE TBL_DocReview_SingleStudy
INNER JOIN TBL_LOA
ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member
INNER JOIN TBL_STUDY
ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.Status
WHERE TBL_STUDY.CDB = 'B'
Can anyone see what I'm doing wrong?
Try:
UPDATE (TBL_DocReview_SingleStudy
INNER JOIN TBL_LOA
ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member)
INNER JOIN TBL_STUDY
ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.Status
WHERE TBL_STUDY.CDB = 'B'
Try the following:
UPDATE s
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.STATUS
FROM TBL_DocReview_SingleStudy S
INNER JOIN TBL_LOA ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member
INNER JOIN TBL_STUDY ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
WHERE TBL_STUDY.CDB = 'B'

Access Syntax Error

I'm attempting to join multiple tables in my Access database. I continue to receive a "syntax error". Please let me know if you need more information. I posted the schema below.
SELECT VicDescriptions.VID,
VicDescriptions.Make,
VicDescriptions.Vic_Year,
VicDescriptions.OptionTable,
VacDescriptions.Accessory,
VacValues.Value,
VacValues.ValueType
FROM VicDescriptions
INNER JOIN VacValues
ON ( VicDescriptions.Vic_Make = VacValues.Vic_Make
AND VicDescriptions.Vic_Year = VacValues.Vic_Year )
INNER JOIN VacDescriptions
ON ( VacDescriptions.Period = VacValues.Period
AND VacDescriptions.VAC = VacValues.VAC);
DATABASE SCHEMA
MS Access like parentheses for joins:
SELECT . . .
FROM (VicDescriptions INNER JOIN
VacValues
ON VicDescriptions.Vic_Make = VacValues.Vic_Make AND
VicDescriptions.Vic_Year = VacValues.Vic_Year
) INNER JOIN
VacDescriptions
ON VacDescriptions.Period = VacValues.Period AND
VacDescriptions.VAC = VacValues.VAC

JOIN syntax and order for multiple tables

SQL Gurus,
I have a query that uses the "old" style of join syntax as follows using 7 tables (table and column names changed to protect the innocent), as shown below:
SELECT v1_col, p1_col
FROM p1_tbl, p_tbl, p2_tbl, p3_tbl, v1_tbl, v2_tbl, v3_tbl
WHERE p1_code = 1
AND v1_code = 1
AND p1_date >= v1_date
AND p_uid = p1_uid
AND p2_uid = p1_uid AND p2_id = v2_id
AND p3_uid = p1_uid AND p3_id = v3_id
AND v2_uid = v1_uid
AND v3_uid = v1_uid
The query works just fine and produces the results it is supposed to, but as an academic exercise, I tried to rewrite the query using the more standard JOIN syntax, for example, below is one version I tried:
SELECT V1.v1_col, P1.p1_col
FROM p1_tbl P1, v1_tbl V1
JOIN p_tbl P ON ( P.p_uid = P1.p1_uid )
JOIN p2_tbl P2 ON ( P2.p2_uid = P1.p1_uid AND P2.p2_id = V2.v2_id )
JOIN p3_tbl P3 ON ( P3.p3_uid = P1.p1_uid AND P3.p3_id = V3.v3_id )
JOIN v2_tbl V2 ON ( V2.v2_uid = V1.v1_uid )
JOIN v3_tbl V3 ON ( V3.v3_uid = V1.v1_uid )
WHERE P1.p1_code = 1
AND V1.v1_code = 1
AND P1.p1_date >= V1.v1_date
But, no matter how I arrange the JOINs (using MS SQL 2008 R2), I keep running into the error:
The Multi-part identifier "col-name" could not be bound,
where "col-name" varies depending on the order of the JOINs I am attempting...
Does anyone have any good examples on how use the JOIN syntax with this number of tables??
Thanks in advance!
When you use JOIN-syntax you can only access columns from tables in your current join or previous joins. In fact it's easier to write the old syntax, but it's more error-prone, e.g. you can easily forget a join-condition.
This should be what you want.
SELECT v1_col, p1_col
FROM p1_tbl
JOIN v1_tbl ON p1_date >= v1_date
JOIN v2_tbl ON v2_uid = v1_uid
JOIN v3_tbl ON v3_uid = v1_uid
JOIN p_tbl ON p_uid = p1_uid
JOIN p2_tbl ON p2_uid = p1_uid AND p2_id = v2_id
JOIN p3_tbl ON p3_uid = p1_uid AND p3_id = v3_id
WHERE p1_code = 1
AND v1_code = 1
You are not naming the tables in your join such that it doesn't know which column is from which table. Try something like:
SELECT a.v1_col, b.p1_col
FROM p1_tbl b
JOIN p_tbl a ON b.p_uid = a.p1_uid
WHERE b.p1_code = 1
From your query above, I am assuming a naming convention of p2_uid comes from p2_tbl. Below id my best interpretation of WHERE joins to using INNER joins.
SELECT
v1_col, p1_col
FROM
p1_tbl
INNER JOIN p1_tbl
ON p1_tbl.p1_date >= v1_tbl.v1_date
INNER JOIN p_tbl
ON p_tbl.p_uid = p1_tbl.p1_uid
INNER JOIN p2_tbl
ON p2_tbl.p2_uid = p1_tbl.p1_uid
INNER JOIN v2_tbl
ON p2_tbl.p2_id = v2_tbl.v2_id
INNER JOIN p3_tbl
ON p3_tbl.p3_uid = p1_tbl.p1_uid
INNER JOIN v3_tbl
ON p3_tbl.p3_id = v3_tbl.v3_id
INNER JOIN v1_tbl
ON v1_tbl.v1_uid = v2_tbl.v2_uid
AND v1_tbl.v1_uid = v3_tbl.v2_uid
WHERE
p1_code = 1
AND
v1_code = 1
Some general points I have found useful in SQL statements with many joins.
Always fully qualify the names. I.e dont use ID , rahter use
TableName.ID
Dont use aliases unless there is meaning. (I.e. joining a table to
its self where aliasing is needed.)

SQL Server 'FOR XML PATH' in PHP/MySQL

I have a SQL Server query that I need to convert to MySQL. I've never used SQL Server/T-SQL before, so I have no experience with FOR XML PATH. There's surprisingly little documentation on this sort of thing. If I remove the FOR XML PATH statement, MySQL returns the error "Operand should contain at least 1 column(s)."
It seems like the nested SELECT statements return strings containing raw XML data, but I don't have the original SQL Server database available to test that theory. I could emulate this effect easily if I knew the return schema.
The original query is below:
SELECT
har_autos.*, har_automodels.Model, har_automodels.MakeID, har_automakes.Make, har_autotypes.AutoType,
(SELECT har_notes.*, har_notelinks.Value
FROM har_notes
JOIN har_notelinks on (har_notes.ID = har_notelinks.NoteID)
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 1)
AS 'UserNotes',
(SELECT har_notes.*, har_notelinks.Value
FROM har_notes
JOIN har_notelinks on (har_notes.ID = har_notelinks.NoteID)
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 2)
AS 'EngineeringNotes'
FROM har_autos
LEFT JOIN har_automodels ON (har_autos.ModelID = har_automodels.ID)
LEFT JOIN har_automakes ON (har_automodels.MakeID = har_automakes.ID)
LEFT JOIN har_autotypes ON (har_autos.AutoTypeID = har_autotypes.ID)
UPDATE:
I rebuilt the query in its entirety and it now returns "Not unique table/alias: 'har_automakes'."
SELECT
har_autos.*,
har_automodels.Model,
har_automodels.MakeID,
har_automakes.Make,
har_autotypes.AutoType,
(SELECT
har_notes.ID,
har_notes.NoteTypeID,
har_notes.Text001,
har_notelinks.Value,
har_notelinks.AutoID,
har_autos.ID
FROM
har_notes
INNER JOIN har_notelinks ON (har_notes.ID = har_notelinks.NoteID),
har_autos
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 1)
AS UserNotes,
(SELECT
har_notes.ID,
har_notes.NoteTypeID,
har_notes.Text001,
har_notelinks.Value,
har_notelinks.AutoID,
har_autos.ID
FROM
har_notes
INNER JOIN har_notelinks ON (har_notes.ID = har_notelinks.NoteID), har_autos
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 2)
AS EngineeringNotes
FROM
har_autos
LEFT OUTER JOIN har_automodels ON (har_autos.ModelID = har_automodels.ID)
LEFT OUTER JOIN har_autotypes ON (har_autos.AutoTypeID = har_autotypes.ID),
har_automakes
LEFT OUTER JOIN har_automakes ON (har_automakes.MakeID = har_automakes.ID)
WHERE
ID = 1
This is the original T-SQL query (really sorry about all of these):
SELECT a.*, mo.Model, mo.MakeID, ma.Make, at.AutoType,
(SELECT n.*, nl.Value
FROM dbo.Notes n
JOIN dbo.NoteLinks nl on (n.ID = nl.NoteID)
WHERE a.ID = nl.AutoID AND n.NoteTypeID = 1
FOR XML PATH('Note'), TYPE
) AS 'UserNotes',
(SELECT n.*, nl.Value
FROM dbo.Notes n
JOIN dbo.NoteLinks nl on (n.ID = nl.NoteID)
WHERE a.ID = nl.AutoID AND n.NoteTypeID = 2
FOR XML PATH('Note'), TYPE
) AS 'EngineeringNotes'
FROM dbo.Autos a
LEFT JOIN dbo.AutoModels mo ON (a.ModelID = mo.ID)
LEFT JOIN dbo.AutoMakes ma ON (mo.MakeID = ma.ID)
LEFT JOIN dbo.AutoTypes at ON (a.AutoTypeID = at.ID)
Little Documentation:
Using PATH Mode
Basic Syntax of the FOR XML Clause
Examples: Using PATH Mode
Constructing XML Using FOR XML
All of the above are from the following search: http://social.technet.microsoft.com/Search/en-US/?Refinement=129&Query=for+xml+path.