MYSQL Syntax Error LEFT JOIN - mysql

Im having issues trying to figure out this syntax error. Heres the SQL query:
SQL QUERY
SELECT oh.date_modified, oh.physicianNote, os.name AS status
FROM order oh
LEFT JOIN order_status os ON oh.order_status_id = os.order_status_id
WHERE oh.order_id = '118' AND os.language_id = '1'
ORDER BY oh.date_added ASC LIMIT 0,10
SQL 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 'order oh LEFT JOIN order_status os ON oh.order_status_id =
os.order_status_i' at line 2
Not really sure whats wrong with it.

ORDER is a reserved word. Quote it in backticks:
SELECT oh.date_modified, oh.physicianNote, os.name AS status
FROM `order` oh LEFT JOIN order_status os USING (order_status_id)
WHERE oh.order_id = '118' AND os.language_id = '1'
ORDER BY oh.date_added
LIMIT 0,10

"ORDER" is reserved word. This is error. Use the word order Quote in backticks

Related

MySQL subquery in select clause

SELECT op.*,
(op.total + op.total * 0.21) as price,
(SELECT p.`image` FROM oc_product` p WHERE op.product_id = p.product_id LIMIT 1) AS image
FROM `oc_order_product` op WHERE op.order_id = '80'
I can't found what is wrong with my query it's returning.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'oc_order_productop WHERE op.order_id = '80' LIMIT 0, 25' at
line 3
Add a back tick before oc_product, so
SELECT p.`image` FROM oc_product`
should look like
SELECT p.`image` FROM `oc_product`

Cant find mySQL request error

Rediculous but I cant find error in this request
SELECT * FROM diploms
LEFT JOIN student ON diploms.student_id = student.student_id
LEFT JOIN group ON student.group_id = group.group_id
LEFT JOIN speciality ON group.speciality_id = speciality.speciality_id
ORDER BY (CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END), speciality.name ASC
But SQL says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group ON student.group_id = group.group_id LIMIT 0, 30' at line 3
WTH?
group is a reserved keyword in MySQL and needs to be escaped by backticks.
SELECT *
FROM diploms
LEFT JOIN student ON diploms.student_id = student.student_id
LEFT JOIN `group` ON student.group_id = `group`.group_id
LEFT JOIN speciality ON `group`.speciality_id = speciality.speciality_id
ORDER BY CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END,
speciality.name ASC
Its not good to use reserved keyword as a table or a column name..
group is a reserved keyword and that's why giving an error, you can use quotes tilt (`) to use it as a table name.
Also you can't use it as column name, see the related post:
group as a column name in insert command

#1064 - check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY word.pl ASC LIMIT 0, 30' at line 1

My query:
SELECT id, pl, ka, user, users.imie, users.id, users.nazwisko, users.klasa
FROM word
LEFT JOIN users ON word.user = users.id BY word.pl ASC
It gives me 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
'BY word.pl ASC LIMIT 0, 30' at line 1
Missing Order in Order By
ALSO Both tables have an id column, so you need to call either/both users.id and word.id to avoid the ambiguous error:
SELECT users.id, word.id, pl, ka, user, users.imie, users.id, users.nazwisko, users.klasa
FROM word
LEFT JOIN users ON word.user = users.id
ORDER BY word.pl ASC
You forgot ORDER:
ORDER BY word.pl ASC
Add Order by clause
SELECT id, pl, ka, user, users.imie, users.id, users.nazwisko, users.klasa
FROM word
LEFT JOIN users ON word.user = users.id
ORDER BY word.pl ASC

How to use column value as parameter for mysql function?

Can anyone help me with this sql statement?
SELECT c.id AS 'cat_id', c.name, sol.id,
(SELECT f.id
FROM folders f, category_folder_mapping cf
WHERE cf.folder_id = f.id AND cf.category_id = c.id
ORDER BY f.id ASC LIMIT 1) AS 'folder_id'
FROM categories c
OUTER APPLY folderFirstSolution(folder_id) sol
ORDER by c.id ASC
My goal in this sql is to use the 'folder_id' as parameter for the folderFirstSolution() function. I always get an sql syntax error.
Please tell me what's wrong with this statement.
Thanks ahead.
(EDIT)
MYSQL 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 'OUTER APPLY folderFirstSolution(folder_id) sol
ORDER by c.id ASC
LIMIT 0, 25' at line 7

MySQL JOIN with WHERE

I have the following query:
SELECT * FROM
(SELECT t1.`id`, t1.`vehicle`, lp1.`veh_no` AS `lp_vehicle`,
t1.`last_date`, t1.`due_date`, t1.`driver`, lp4.`employ_name` AS `lp_driver`
FROM `inspection` AS t1 LEFT OUTER JOIN `vehicle` AS lp1
ON (t1.`vehicle` = lp1.`id`)
LEFT OUTER JOIN `employee_driver` AS lp4
ON (t1.`driver` = lp4.`id`)) subq,
WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER by vehicle asc;
It processes through fine until I get to the WHERE clause.
This is what I get on the above:
#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
'WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER b'
at line 1
Can someone please point out what I am doing wrong? I'm running MySQL 5.1.48
you have an extra comma after subq