Syntax error while select query in mysql [duplicate] - mysql

This question already has answers here:
You have an error in your SQL syntax; near 'order(cust_id, order_date,order_total, order_state,order_city,order_add,order' at line 1
(2 answers)
Closed 6 years ago.
I am new to coding. I am executing this query:
SELECT order.*, item.*
FROM tbl_orders order
LEFT JOIN tbl_order_items item ON order.id=item.order_id
WHERE order.id=1;
How far my understanding this query is correct. But this error is occurring don't know why.
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.* , item.* FROM tbl_orders order LEFT JOIN tbl_order_items item on order.i' at line 1
Can any one help me.

This is because you are using reserved word order of mysql.
You can change the alias to something else and this will solved, Something like this:
SELECT ord.* , item.* FROM tbl_orders ord LEFT JOIN tbl_order_items item on ord.id=item.order_id WHERE ord.id=1;
For more you can see this link:http://www.inmotionhosting.com/support/website/database-troubleshooting/error-1064

Do another 'other' word.
I change to orders for example.
SELECT orders.* , item.* FROM tbl_orders orders LEFT JOIN tbl_order_items item on orders.id=item.order_id WHERE orders.id=1;

Related

SQL syntax error for deleting multiple rows from a subquery

I have a relational MySQL database using InnoDB which connects courses to course attendees. The problem with this database is that the course_id column in course_attendees was not set as foreign key. There are alot of courses missing where the course_attendees table is trying to refer to.
I wanted to delete those records, since the courses don't belong anymore. I wrote this select query which selects all the courses that should be deleted:
SELECT
ca.`id`
FROM `course_attendees` AS ca
LEFT JOIN `courses` c
ON ca.`course_id` = c.`id`
WHERE c.`id` IS NULL
Now, when I try to wrap this in a DELETE query with the use of a subquery like this:
DELETE FROM courses AS C1
WHERE C1.`id` IN (
SELECT
ca.`id`
FROM `course_attendees` AS ca
LEFT JOIN `courses` c
ON ca.`course_id` = c.`id`
WHERE c.`id` IS NULL
);
I get the following error:
[2018-08-30 08:34:26] [42000][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 'AS C1
[2018-08-30 08:34:26] [42000][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 'AS C1
[2018-08-30 08:34:26] WHERE C1.`id` IN (
[2018-08-30 08:34:26] SELECT
[2018-08-30 08:34:26] ca.`id`
[2018-08-30 08:34:26] FROM `course_attendees` AS c' at line 1
Since the SELECT query works, what is the problem here and how can I fix it?
EDIT
After Tim's answer, it got me into this error:
[HY000][1093] You can't specify target table 'courses' for update in FROM clause
Your outer delete query is not correlated to the subquery at all, so you don't logically need aliases:
DELETE
FROM courses
WHERE id IN (
SELECT id FROM
(
SELECT ca.id
FROM course_attendees AS ca
LEFT JOIN courses c
ON ca.course_id = c.id
WHERE c.id IS NULL
) t
);
I'm not sure that aliases are allowed in a delete statement on just a single table. They are allowed for a delete join, but you're not doing that.

I don't know why I'm getting this error in full join in MySql [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 4 years ago.
select w.fname,w.salary, t.worker_title from Worker w full join Title t on w.worker_id=t.worker_ref_id;
ERROR 1064 (42000): 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 'full join Title t on w.worker_id=t.worker_ref_id' at line 1
if you want result of full ouret join then use below way:
SELECT p.LastName, p.FirstName, o.OrderNo
FROM persons AS p
LEFT JOIN
orders AS o
ON o.orderNo = p.p_id
UNION ALL
SELECT NULL, NULL, orderNo
FROM orders
WHERE orderNo NOT IN
(
SELECT p_id
FROM persons
)
Dude try worker as w INNER JOIN title as t

Error when executing a FULL OUTER JOIN query in MySQL [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 5 years ago.
Hello I am trying to execute a query in mysql but i am getting following error
query :-
SELECT * FROM user_registration FULL OUTER JOIN user_details ON user_registration.email = user_details.gmail
error is :
#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 'OUTER JOIN dreams ON user_registration.email = user_details.gmail LIMIT 0, 25' at line 1
As there is no full outer join in MySQL, you can use left and right outer joins with union:
select * from user_registration ur left join user_details ud on ur.email = ud.gmail
union
select * from user_registration ur right join user_details ud on ur.email = ud.gmail

mysql left join syntax error 1064

Really stumped on this one. It should be a simple left join to a second table but i'm getting a syntax error. Trying to get the last due date on incomplete items.
Code:
SELECT *
FROM TBLTICKETHEADER h,
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) ld ON ld.HEADERID = h.HEADERID
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 'LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETIT' at line 3
You have a comma after your h on the from clause. Remove it and your query should run.
SELECT *
FROM TBLTICKETHEADER h
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) AS ld ON ld.HEADERID = h.HEADERID

Query error when testing in Database SQL

I have this query and appearently it's faulty?
I'm trying to join fices with mems so I can have the ficeID along with all the results from mems(These queries work individually). What am I doing wrong?
SELECT *
FROM mems
WHERE deleted <> -1
ORDER BY sort_mems
LEFT JOIN SELECT ficeID
FROM fices
Result:
#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 'LEFT JOIN SELECT ficeID FROM offices LIMIT 0, 30' at line 1
You have JOIN clause after ORDER BY. You should place it in FROM.
I suggest you define condition of a LEFT JOIN
Also, I suggest you surround you temp tables with brackets:
SELECT m.*, t1.officeID
FROM members m
LEFT JOIN offices t1
ON m.memberID = t1.memberID
WHERE m.deleted <> -1
ORDER BY m.sort_membername;
Yes, you have the LEFT JOIN in the wrong spot (it should go after your FROM clause, and you also seem to be missing a join criteria (the ON part, this tells the database how these tables are related):
SELECT *
FROM mems m
LEFT JOIN fices f
ON m.??? = f.???
WHERE deleted <> -1
ORDER BY sort_mems