Can't figure out this syntax error in SQL SELECT statement - mysql

I have been trying to get a simple SELECT statement to work but I keep getting a syntax error on the LEFT JOIN. The code:
SELECT
p.player_name,
s.country_name
FROM player_mast p
WHERE p.team_id=1217
LEFT JOIN soccer_country s ON p.team_id = s.country_id;
Seems pretty simple i guess, but it keeps giving me this error:
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'LEFT JOIN soccer_country AS s ON p.team_id =
s.country_id' at line 6
I'm using MySQL 8.0 and I have checked the documentation. I am a beginner with SQL so I'm sure I'm missing something obvious, but I just can't figure out what..
Help is much appreciated.

JOIN clause should come before the WHERE clause.
SELECT
p.player_name,
s.country_name
FROM player_mast p
LEFT JOIN soccer_country s ON p.team_id = s.country_id
WHERE p.team_id=1217;

Related

INTERSECT in mysql cant running

select
pengarang_id,buku_judul,penerbit_id
from buku
left join bukupengarang on buku.penerbit_id = bukupengarang.pengarang_id
INTERSECT
select
pengarang_id,buku_judul,penerbit_id
from buku right join bukupengarang on buku.penerbit_id = bukupengarang.pengarang_id;`
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERSECT
select pengarang_id,buku_judul,penerbit_id
from buku
right join bukup' at line 5
could anyone tell me whts problem here?
i dont understand abt this one, i just read it on inet n still didnt get it
MySQL does not support the INTERSECT operator. You can emulate the INTERSECT operator in MySQL using join clauses.
https://www.mysqltutorial.org/mysql-intersect/
It works with MariaDB since version 10.3.0.

MySQL query with a JOIN and WHERE clause not working

I'm trying to get a MySQL query to work but keep getting an error. I want to join two tables tbl_collab and tbl.uploads with a WHERE clause but can't figure out what I've got wrong. Thanks.
SELECT tbl_collab.collab_userid, tbl_collab.file, tbl_collab.tbl_upload_id,
tbl_uploads.id,tbl_uploads.title,
FROM tbl_uploads
LEFT JOIN tbl_uploads.id ON tbl_collab.tbl_upload_id
WHERE tbl_collab.collab_userid='2'
I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM tbl_uploads LEFT JOIN tbl_uploads.id ON tbl_collab.tbl_upload_id WHERE ' at line 3
Your whole syntax after from and left join are wrong.
Please update it as per below query.
Try this:
SELECT tbl_collab.collab_userid,
tbl_collab.file,
tbl_collab.tbl_upload_id,
tbl_uploads.id,
tbl_uploads.title
FROM tbl_collab
LEFT JOIN tbl_uploads ON tbl_uploads.id = tbl_collab.tbl_upload_id
WHERE tbl_collab.collab_userid='2'
There is a comma (,) in your SELECT clause after tbl_uploads_title that should be removed.
SELECT tbl_collab.collab_userid, tbl_collab.file, tbl_collab.tbl_upload_id, tbl_uploads.id,tbl_uploads.title FROM tbl_uploads LEFT JOIN tbl_uploads.id ON tbl_collab.tbl_upload_id WHERE tbl_collab.collab_userid='2`
Just remove comma before FROM

1064 You have an error in your SQL syntax; - shows in my website

I was working on my new joomla website changed some appearing sittings then the syntax error came out in the main page and changed all the website design I tried to undo all the changes I made and the error still there
the message I got
1064 You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ') AND c.access_view IN (0,1,1,5) AND c.restriction_18=0 ORDER BY
i.date_start D' at line 2
SELECT i.*,c.id as c_id, c.name as c_name,c.alias as c_alias,c.icon_url as c_icon_url,
r.name as r_name, img.path as img_path, img.name as img_name, img.ext as img_ext,
img.caption as img_caption
FROM l0ucm_djcf_categories c, l0ucm_djcf_items i
LEFT JOIN l0ucm_djcf_regions r ON r.id=i.region_id
LEFT JOIN ( SELECT img.id, img.item_id, img.name, img.path, img.ext, img.ordering, img.caption
FROM (SELECT * FROM l0ucm_djcf_images WHERE type='item' ORDER BY ordering) img GROUP BY img.item_id ) AS img
ON img.item_id=i.id
WHERE i.date_exp > '2015-07-25 16:44:45' AND i.published = 1 AND c.published = 1
AND i.cat_id=c.id AND i.cat_id IN (9,10,11,12,13,15)
AND i.type_id IN () AND c.access_view IN (0,1,1,5) AND c.restriction_18=0
ORDER BY i.date_start DESC limit 9
anyone can help me to find out from where I can solve this problem?
thank you all,
The problem is in the statement AND i.type_id IN ().
IN() function cannot get an empty value. You should check the php code that creates the query and add a validation to the variable to add the statement only if it not empty.

Building MySQL Query with Access

I have the following query and it comes up with an error
SELECT CRM_PRESUPUESTOS.Fecha_Alta, CRM_PRESUPUESTOS.ID_VendedorAsignado, Sum([Precio]*(100-[CRM_PresupuestosDetalles].[Bonif])/100*[CRM_PresupuestosDetalles].[Cantidad]) AS LineaNeto
FROM CRM_PRESUPUESTOS RIGHT JOIN CRM_PresupuestosDetalles ON CRM_PRESUPUESTOS.ID_Presupuesto = CRM_PresupuestosDetalles.ID_Presupuesto
GROUP BY CRM_PRESUPUESTOS.Fecha_Alta, CRM_PRESUPUESTOS.ID_VendedorAsignado
HAVING ((DATE((CRM_PRESUPUESTOS.Fecha_Alta))=CurDate()));
The error is
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Precio](100-[CRM_PresupuestosDetalles].[Bonif])/100[CRM_PresupuestosDetalles‌​]' at line 1
How can I fix this? The problem probably is that I'm building them with Access
Square brackets, [ ], are t-sql specific. They tell the parser that the contained text is a string and keeps you safe from potentially mistakenly using a t-sql reserved word. The MySql similar way of doing this is with backticks: `
SELECT CRM_PRESUPUESTOS.Fecha_Alta,
CRM_PRESUPUESTOS.ID_VendedorAsignado,
Sum(`Precio`*(100-`CRM_PresupuestosDetalles`.`Bonif`)/100*`CRM_PresupuestosDetalles`.`Cantidad`) AS LineaNeto
FROM CRM_PRESUPUESTOS RIGHT JOIN CRM_PresupuestosDetalles ON CRM_PRESUPUESTOS.ID_Presupuesto = CRM_PresupuestosDetalles.ID_Presupuesto
GROUP BY CRM_PRESUPUESTOS.Fecha_Alta, CRM_PRESUPUESTOS.ID_VendedorAsignado
HAVING ((DATE((CRM_PRESUPUESTOS.Fecha_Alta))=CurDate()));
TRy this::
SELECT
CRM_PRESUPUESTOS.Fecha_Alta,
CRM_PRESUPUESTOS.ID_VendedorAsignado,
Sum(Precio*(100-CRM_PresupuestosDetalles.Bonif/100*CRM_PresupuestosDetalles.Cantidad) AS LineaNeto
FROM CRM_PRESUPUESTOS RIGHT
JOIN CRM_PresupuestosDetalles ON CRM_PRESUPUESTOS.ID_Presupuesto = CRM_PresupuestosDetalles.ID_Presupuesto
GROUP BY CRM_PRESUPUESTOS.Fecha_Alta, CRM_PRESUPUESTOS.ID_VendedorAsignado
HAVING DATEDIFF(CRM_PRESUPUESTOS.Fecha_Alta, CurDate)=0

MySQL query with date

I have following query, but it gives me errors, if anyone could give me a hint, would be awesome.
SELECT tblinvoices.*,companyname FROM tblinvoices INNER JOIN tblclients
ON tblclients.id=tblinvoices.clientid
WHERE 1=1 AND date between '20111201' to '20111208'
The error message is:
Error 1064: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'TO '20111208''
use AND instead of TO in the BETWEEN command.
SELECT tblinvoices.*,companyname FROM tblinvoices INNER JOIN tblclients
ON tblclients.id=tblinvoices.clientid
WHERE 1=1 AND date between '20111201' AND '20111208'
I am pretty sure is the word TO, it should be:
SELECT tblinvoices.*,companyname FROM tblinvoices INNER JOIN tblclients ON tblclients.id=tblinvoices.clientid WHERE 1=1 AND date between '20111201' AND '20111208'
If companyname is from tblinvoices it should work, otherwise you need to check where companyname comes from. And the syntax for between is like this
date between '20111201' and '20111208'