MySQL query with date - mysql

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'

Related

mysql left join complaning

Can someone please tell me what is syntactically wrong with this query?
The error message returned is not helping at all.
select P.test_result_calc_category, a.NumberofPatients from (select distinct test_result_calc_category FROM a1c) P
left join
(select test_result_calc_category, count (distinct patient_id) as NumberofPatients from a1c group by test_result_calc_category) a
ON P.test_result_calc_category = a.test_result_calc_category
#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 'distinct patient_id) as NumberofPatients from a1c group by test_result_calc_cate' at line 5
Remove the space after count
count(distinct patient_id)
^----------here
See https://dev.mysql.com/doc/refman/8.0/en/function-resolution.html
You have a space after the function COUNT in COUNT (DISTINCT ...
There are rules about this. You should have no space before the ( unless you set the sql_mode=IGNORE_SPACE.

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

SQL query: join two tables

so i get an error on the this sql query, but i can´t see my mistake myself:
SELECT group_members.group_id,
group_members.permissions,
group.group_name
FROM group_members,
group
WHERE group_members.group_id=group.group_id
AND group.group_id = 1
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 'group WHERE group_members.group_id=group.group_id AND group.group_id = 1
Thanks for your help!
Group is a reserved word in MySQL either enclose it in backticks "`" or better yet do not use it as a table name
SELECT group_members.group_id, group_members.permissions, `group`.group_name
FROM group_members, `group`
WHERE group_members.group_id=`group`.group_id
AND `group`.group_id = 1
Try this
SELECT group_members.group_id, group_members.permissions, `group`.group_name
FROM group_members, group
WHERE group_members.group_id=`group`.group_id
AND `group`.group_id = 1

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 join 2 database and 3 tables query?

SELECT db1_t1.userid as userid
, db1_t1.customer_id as vw_customer
, db2_t1.customers_id as customer
, db2_t1.orders_id as order
FROM database1.table1 db1_t1
LEFT JOIN database2.table1 db2_t1
ON db1_t1.customer_id = db2_t1.customers_id
It gives me this error:
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 'order FROM
database1.table1 db1_t1 LEFT JOIN
database2.' at line 2
I am using php and mysql.
order is a keyword - think ORDER BY my_column.
I'd suggest renaming it, but you could enclose it in backticks
db2_t1.orders_id AS `order`