MySQL subquery in select clause - mysql

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`

Related

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 - You have an error in your SQL syntax: What is wrong with this query?

Please have a look at the below query.
SELECT sub_words.idwords, words_inc.idArticle
(
SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS excl_words, COUNT(sub1.idwords) AS older_words_cnt
FROM words_learned sub0
LEFT OUTER JOIN words_learned sub1
ON sub0.userId = sub1.userId
AND sub0.order < sub1.order
WHERE sub0.userId = 1
GROUP BY sub0.idwords
) sub_words
INNER JOIN words words_inc
ON sub_words.idwords = words_inc.idwords
LEFT OUTER JOIN words words_exc
ON words_inc.idArticle = words_exc.idArticle
AND FIND_IN_SET(words_exc.idwords, sub_words.excl_words)
WHERE words_exc.idwords IS NULL
ORDER BY older_words_cnt
LIMIT 100
This gives the 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 'SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS exc' at line 3
I checked the sub query individually and there was no error in the sub query! what is going on here?
You missed the from keyword
SELECT sub_words.idwords, words_inc.idArticle
FROM
( ...

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 Syntax Error LEFT JOIN

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

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