MySQL query with a JOIN and WHERE clause not working - mysql

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

Related

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

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;

MySQL trouble connecting two columns of names

I have two tables that I'm trying to connect. One table is called 2019projections and the other is called 2019actualstat. I want to connect the two by names. I'm 99% sure every name that is in 2019actualstat is in 2019projections, but not every name in 2019actualstat is in 2019projections. The latter has alot more names, but most of them are useless.
I've tried left join and right join.
I've tried select distinct
I gave a shot at exists
This is what I have so far:
USE Fantasyfootball;
SELECT DISTINCT *
FROM 2019actualstat;
LEFT JOIN 2019projections ON
2019actualstat.Player =
2019projections.first_last;
It's giving me the 1064 error, but I think it has to do with the 2019projections table having more records.
21:27:26 LEFT JOIN 2019projections ON 2019actualstat.Player =
2019projections.first_last 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 2019projections ON
2019actualstat.Player = 2019projections.first_last' at line 1 0.00071
sec
2019projections.first_last is a varchar(50) and 2019actualstat.player is text
PS: I have the .csv files which I'm not sure how to post, but I would be happy to send them both.
You're missing the select list, and have a redundant (wrong) semicolon at the end of the from clause:
SELECT *
FROM 2019actualstat
LEFT JOIN 2019projections ON 2019actualstat.Player = 2019projections.first_last;

Update table column with inner join

UPDATE micro_finance_loans AS 'ML' SET ML.Loan_status=11
INNER JOIN micro_finance_customers AS 'MC' on MC.Customer_Id=ML.Customer_Id
WHERE ML.loan_status=10 AND MC.Number='4410188992243'
Can someone please tell me whats the error in this query, error as:
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 'inner join microfinance_customers as mc on mc.CustomerId=ml.CustomerId
where ml' at line 2
UPDATE micro_finance_loans AS ML
INNER JOIN micro_finance_customers AS MC on MC.Customer_Id=ML.Customer_Id
SET ML.Loan_status=11
WHERE ML.loan_status=10 AND MC.Number='4410188992243'
Try this instead
UPDATE micro_finance_loans AS 'ML' SET ML.Loan_status=11 from micro_finance_loans
INNER JOIN micro_finance_customers AS 'MC' on MC.Customer_Id=ML.Customer_Id
WHERE ML.loan_status=10 AND MC.Number='4410188992243'

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'