Using Limit on Delete SQL queries - mysql

I had this query
DELETE c FROM review_comments AS c
LEFT JOIN users AS u
ON u.user_id = c.user_id
WHERE c.comment = '{$comment}'
AND u.username = '{$user}'
LIMIT 1;
It did not work until I removed LIMIT 1;
It said: 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 'LIMIT 1' at line 6
Is this the incorrect usage of LIMIT 1 in this instance? I had a same query without joining tables and LIMIT 1 worked fine?

I don't think LIMIT can be used with multi-table referenced DELETE statements in MySQL.

Related

How do I run this script in MYSQL?

This is my script from Oracle but I have problems running it on MySQL.
SELECT s.branch_no, SUM(s.staff_salary)
from staff AS s
HAVING SUM(staff_salary) = (SELECT MAX(SUM(S2.staff_salary))
FROM staff AS S2
GROUP BY S2.BRANCH_NO)
GROUP BY s.branch_no;
The error that I get when I run it on MySQL:
SELECT s.branch_no, SUM(s.staff_salary)
from staff AS s
HAVING SUM(staff_salary) = (SELECT MAX(SUM(S2.staff_salary))
FROM staff AS S2
GROUP BY S2.branch_no)
GROUP BY s.branch_no
LIMIT 0, 50
MySQL said:
#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 BY s.branch_no LIMIT 0, 50' at line 7
Try seperating the query into small subqueries.
In your case I would try:
SELECT *
FROM (
SELECT SUM(staff_salary) sm , branch_no
FROM staff
GROUP BY BRANCH_NO
) s
ORDER BY sm DESC LIMIT 1

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

INNER JOIN syntax for mySQL using phpmyadmin

SELECT Question.userid, user.uid
FROM `question`
WHERE NOT `userid`=2
LIMIT 0, 60
INNER JOIN `user`
ON `question`.userid=`user`.uid
ORDER BY `question`.userid
returns 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 'INNER JOIN User ON question.userid=user.uid ORDER BY question.userid' at line 5
Can't for the life of me figure out what I'm doing wrong here.
Your query doesn't look valid. You may want to try the following:
SELECT `question`.userid, `user`.uid
FROM `question`
INNER JOIN `user` ON `question`.userid = `user`.uid
WHERE `userid` <> 2
ORDER BY `question`.userid
LIMIT 0, 60