Mysql join 2 database and 3 tables query? - mysql

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`

Related

SQL execution error

I try this query
INSERT INTO shop.product(prod_id,model,desc) SELECT product.id_prod,prod_lang.name,product.ref from product left join product_lang on product_lang.id_prod = product.id_prod
However I got this error
SQL execution error # 1065.Response from the database:
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') SELECT product.id_prod,product_lang.name,product.ref from' at line 1
Two problems:
DESC is a reserved keyword. Use backquote (``) for desc.
Change prod_lang to product_lang in the query.
Solution:
INSERT INTO shop.product (prod_id,model,`desc`)
SELECT product.id_prod,product_lang.name,product.ref
from product left join
product_lang on product_lang.id_prod = product.id_prod
Note:
It is a good practice to use backquotes for all columns eventhough it is not a reserved keyword.

What is wrong with this MySQL query? right syntax for INNER_JOIN WHERE clause?

SELECT core_student.STUDENT_ID, core_student.FIRST_NAME, core_student.LAST_NAME
FROM `core_student`
INNER_JOIN `ssp_student`
WHERE ssp_student.STUDENT_ID = core_student.STUDENT_ID;
throws an error on the WHERE clause:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to user near 'ssp_student' WHERE ssp_student.STUDENT_ID = core_student.STUDENT_ID
I just want to select the fields listed in the SELECT, then join all columns from ssp_student where the ssp_student.STUDENT_ID field is the same as the core_student.STUDENT_ID field.
Correct way is
SELECT core_student.STUDENT_ID, core_student.FIRST_NAME, core_student.LAST_NAME
FROM `core_student`
INNER JOIN `ssp_student`
on ssp_student.STUDENT_ID = core_student.STUDENT_ID;

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 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'