I'm trying to join three tables. But I get this error
Syntax error(missing operator) in query expression STUDENTAI.mėgstamiausia_laida = TV_LAIDOS.id
LEFT JOIN MIESTAI
ON STUDENTAI.kilme = MIESTAI.koda'.
Here is my code
SELECT MIESTAI.pavadinimas, TV_LAIDOS.pavadinimas
FROM STUDENTAI
LEFT JOIN TV_LAIDOS
ON STUDENTAI.mėgstamiausia_laida = TV_LAIDOS.id
LEFT JOIN MIESTAI
ON STUDENTAI.kilme = MIESTAI.kodas
WHERE STUDENTAI.ugis > 190;
So what's wrong? Why I get this error?
This query:
SELECT MIESTAI.pavadinimas, TV_LAIDOS.pavadinimas
FROM STUDENTAI LEFT JOIN
TV_LAIDOS
ON STUDENTAI.mėgstamiausia_laida = TV_LAIDOS.id LEFT JOIN
MIESTAI
ON STUDENTAI.kilme = MIESTAI.kodas
WHERE STUDENTAI.ugis > 190;
Looks structurally correct for any database . . . except MS Access. In that system, you need parentheses around the joins:
SELECT MIESTAI.pavadinimas, TV_LAIDOS.pavadinimas
FROM (STUDENTAI LEFT JOIN
TV_LAIDOS
ON STUDENTAI.mėgstamiausia_laida = TV_LAIDOS.id
) LEFT JOIN
MIESTAI
ON STUDENTAI.kilme = MIESTAI.kodas
WHERE STUDENTAI.ugis > 190;
Note that this assumes that the tables and columns all exist and accented characters are allowed in column names and so on.
Your syntax is correct. Either the table MIESTAI doesn't exist, or the field names STIDENTAI.kilme, or MIESTAI.kadas doesn't exist.
Do you really want to join kilme and kadas?
This column name looks special:
STUDENTAI.mėgstamiausia_laida
It ran on my MySQL if I enclosed it in ``, i.e.
SELECT MIESTAI.pavadinimas, TV_LAIDOS.pavadinimas
FROM STUDENTAI
LEFT JOIN TV_LAIDOS
ON STUDENTAI.`mėgstamiausia_laida` = TV_LAIDOS.id
LEFT JOIN MIESTAI
ON STUDENTAI.kilme = MIESTAI.kodas
WHERE STUDENTAI.ugis > 190;
I want to make an INNER JOIN between a table variable and a view
SELECT
T. *
FROM
#mytable AS T, Vw_GetPackageStat.DimensionValueName
From
dbo.Vw_GetPackageStat
INNER JOIN
T ON (dbo.Vw_GetPackageStat.AttributeValueID = T.AttributeValueID)
I have this error
Incorrect syntax near the keyword 'from'.
SELECT T.* ,
Vw_GetPackageStat.DimensionValueName
FROM #AllPackage AS T
INNER JOIN dbo.Vw_GetPackageStat ON ( dbo.Vw_GetPackageStat.AttributeValueID = T.AttributeValueID )
Use this
SELECT T.* ,
Vw_GetPackageStat.DimensionValueName
FROM #AllPackage AS T
INNER JOIN dbo.Vw_GetPackageStat ON
Vw_GetPackageStat.AttributeValueID = T.AttributeValueID
I think you were originally trying to do this
SELECT T.*
,Vw_GetPackageStat.DimensionValueName
FROM #mytable AS T
,Vw_GetPackageStat
WHERE dbo.Vw_GetPackageStat.AttributeValueID = T.AttributeValueID
this is a valid syntax but I would avoid it at all cost. Instead use traditional explicit INNER JOIN
SELECT T.*
,v.DimensionValueName
FROM #mytable AS T
INNER JOIN Vw_GetPackageStat AS v
ON T.AttributeValueID = V.AttributeValueID
I'm having trouble getting my query working. Could someone cast an experienced eye on it please? The table structure is simple (2 one-to-many relationships). The query is trying to work out for each sign, how many contributions there are at each unique "PositionLocation".
Sign <- Signifier (f_key sign_oid) <- Contribution (f_key signifier_oid)
I'm getting the following error:
Error: An ON clause associated with a JOIN operator is not valid.
SQLState: 42972
ErrorCode: -1
My query is:
select s.NAME, c.POSITIONLOCATION, count(*) as num_per_locn,
(
select count(*) from APP.CONTRIBUTION c2
inner join APP.SIGNIFIER si2 on si2.OID = c2.SIGNIFIER_OID
inner join APP.SIGN s2 on s2.OID = si2.SIGN_OID
and s2.OID = s.OID
) as num_per_sign
from APP.CONTRIBUTION c
inner join APP.SIGNIFIER si on si.OID = c.SIGNIFIER_OID
inner join APP.SIGN s on s.OID = si.SIGN_OID
group by s.NAME, c.POSITIONLOCATION
I was trying to join 3 tables - CurrentProducts, SalesInvoice and SalesInvoiceDetail. SalesInvoiceDetail contains FK/foreign key to the other two tables and some other columns. The first query is ok but the second is not. My question comes at the end of the code.
Right
select *
from CurrentProducts inner join
(dbo.SalesInvoiceDetail inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
)
on dbo.SalesInvoiceDetail.ProductID = dbo.CurrentProducts.ProductID
Wrong
select *
from CurrentProducts inner join
(select * from
dbo.SalesInvoiceDetail inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
)
on dbo.SalesInvoiceDetail.ProductID = dbo.CurrentProducts.ProductID
error - Incorrect syntax near the keyword 'on'.
Why is the second query wrong ? Isn't it conceptually the same as the first one ? That is inside join makes a result set. We select * the result set and then join this result set to CurrentProducts ?
The first query is a "plain" join expressed with an older syntax. It can be rewritten as:
select
*
from
CurrentProducts
inner join dbo.SalesInvoiceDetail
on dbo.SalesInvoiceDetail.ProductID = dbo.CurrentProducts.ProductID
inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
The second query is a join where the second table is a subquery. When you join on a subquery, you must assign an alias to it and use that alias to refer to the columns returned by the subquery:
select
*
from
CurrentProducts
inner join (select *
from dbo.SalesInvoiceDetail
inner join dbo.SalesInvoice
on SalesInvoiceDetail.InvoiceID = SalesInvoice.InvoiceID
) as foo on foo.ProductID = dbo.CurrentProducts.ProductID
You need to alias the inner query. Also, in the first one the parentheses are not needed.
select *
from CurrentProducts inner join
(select * from
dbo.SalesInvoiceDetail inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
) A
on A.ProductID = dbo.CurrentProducts.ProductID
I have used following code there in SQL
SELECT
Stu_info.Id_num,
Stu_info.Stu_name,
Development_fee.Dvf,
Tuition_fee.Acy,
Tuition_fee.Tui_fee,
Registration_fee.Reg_fee,
Form_fill_up_fee.Acy,
Form_fill_up_fee.FFF,
Examination_fee.E_typ,
Examination_fee.Exm_fee,
monthly_instal.Instm,
monthly_instal.Paid
FROM
[SUIMT].[dbo].[Stu_info],
[SUIMT].[dbo].[Development_fee],
[SUIMT].[dbo].[Tuition_fee],
[SUIMT].[dbo].[Registration_fee],
[SUIMT].[dbo].[Form_fill_up_fee],
[SUIMT].[dbo].[Examination_fee],
[SUIMT].[dbo].[monthly_instal]
WHERE
Development_fee.Id_num,
Tuition_fee.Id_num,
Registration_fee.Id_num,
Form_fill_up_fee.Id_num,
Examination_fee.Id_num,
monthly_instal.Id_num = Stu_info.Id_num
but it shows error which says
"Msg 4145, Level 15, State 1, Line 3
An expression of non-boolean type specified in a context where a
condition is expected, near ','".
Would you please help me to solve this problem? I would be grateful for providing an example so I could understand it easier.
What is the WHERE clause meant to achieve? Do you mean this?
WHERE
Stu_info.Id_num = Development_fee.Id_num
AND Stu_info.Id_num = Tuition_fee.Id_num
AND Stu_info.Id_num = Registration_fee.Id_num
AND Stu_info.Id_num = Form_fill_up_fee.Id_num
AND Stu_info.Id_num = Examination_fee.Id_num
AND Stu_info.Id_num = monthly_instal.Id_num
Also, using FROM tableA, TableB, TableC is a very old style notation. ANSI-92 standard uses JOIN...
SELECT
Stu_info.Id_num, Stu_info.Stu_name, Development_fee.Dvf, Tuition_fee.Acy, Tuition_fee.Tui_fee, Registration_fee.Reg_fee, Form_fill_up_fee.Acy, Form_fill_up_fee.FFF, Examination_fee.E_typ, Examination_fee.Exm_fee, monthly_instal.Instm, monthly_instal.Paid
FROM
[SUIMT].[dbo].[Stu_info]
INNER JOIN
[SUIMT].[dbo].[Development_fee]
ON Stu_info.Id_num = Development_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Tuition_fee]
ON Stu_info.Id_num = Tuition_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Registration_fee]
ON Stu_info.Id_num = Registration_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Form_fill_up_fee]
ON Stu_info.Id_num = Form_fill_up_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Examination_fee]
ON Stu_info.Id_num = Examination_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[monthly_instal]
ON Stu_info.Id_num = monthly_instal.Id_num
You should use proper ANSI SQL-92 JOIN syntax (INNER JOIN, LEFT OUTER JOIN etc.) and table alias to make your query more understandable and easier to read - something like:
SELECT
stu.Id_num, stu.Stu_name,
df.Dvf,
tf.Acy, tf.Tui_fee,
rf.Reg_fee,
ffuf.Acy, ffuf.FFF,
ef.E_typ, ef.Exm_fee,
mi.Instm, mi.Paid
FROM
[SUIMT].[dbo].[Stu_info] stu
INNER JOIN
[SUIMT].[dbo].[Development_fee] df ON df.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Tuition_fee] tf ON tf.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Registration_fee] rf ON rf.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Form_fill_up_fee] ffuf ON ffuf.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Examination_fee] ef ON ef.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[monthly_instal] mi ON mi.Id_Num = stu.Id_num
This way, you don't even need an ugly WHERE clause at all.....
Separate your clauses in where by AND or OR, not a comma
SELECT Stu_info.Id_num, Stu_info.Stu_name, Development_fee.Dvf, Tuition_fee.Acy, Tuition_fee.Tui_fee, Registration_fee.Reg_fee, Form_fill_up_fee.Acy, Form_fill_up_fee.FFF, Examination_fee.E_typ, Examination_fee.Exm_fee, monthly_instal.Instm, monthly_instal.Paid
FROM [SUIMT].[dbo].[Stu_info],
[SUIMT].[dbo].[Development_fee],
[SUIMT].[dbo].[Tuition_fee],
[SUIMT].[dbo].[Registration_fee],
[SUIMT].[dbo].[Form_fill_up_fee],
[SUIMT].[dbo].[Examination_fee],
[SUIMT].[dbo].[monthly_instal]
WHERE Development_fee.Id_num=Tuition_fee.Id_num and
Registration_fee.Id_num=Form_fill_up_fee.Id_num and
Examination_fee.Id_num=monthly_instal.Id_num and
monthly_instal.Id_num=Stu_info.Id_num