I tried to run the following query to select all of the lines respecting the join criteria, then adds all the rows in the table "client" and the table "commande" that were rejected because they did not meet the join criteria.
Query:SELECT * FROM CLIENT c FULL JOIN commande com ON c.id_client=com.id_client
Error:Error Code: 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 'FULL JOIN commande com on c.id_client=com.id_client
LIMIT 0, 1000' at line 1
MySQL unfortunately does not support FULL JOIN. You can achieve the same results with a UNION and an OUTER JOIN though:
SELECT c.*, com.*
FROM CLIENT c
LEFT JOIN commande com ON c.id_client=com.id_client
UNION
SELECT c.*, com.*
FROM commande com
LEFT JOIN CLIENT c ON com.id_client=c.id_client
Simplified SQL Fiddle Demo
Also, consider defining your fields being returned versus returning all.
try to use full outer join or full inner join
try USING:
SELECT * FROM `CLIENT` FULL JOIN commande com USING (id_client) […];
Related
Error Code: 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 Join Mobile ON Plan.PlanName = Mobile.PlanName Where Mobile.PlanName IS NU' at line 3
Select PlanName
From Plan
full Outer Join Mobile
ON Plan.PlanName = Mobile.PlanName
Where Mobile.PlanName IS NULL;
You don't have FULL JOINS on MySQL, but you can emulate them.
SELECT * FROM plan
LEFT JOIN mobile ON plan.id = mobile.id
UNION
SELECT * FROM plan
RIGHT JOIN mobile ON plan.id = mobile.id
Also, you can use left join, right join, left outer join, right outer join, inner join based on your requirements.
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 have two DBs- RATINGSAPP and MIGRATIONDATA.
I want to update a table in RATINGSAPP with some values in a table in MIGRATIONDATA. I am trying to run this query:
update r set internal_id = m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
This gives me 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
'from ratingsapp.hotel03 as r inner join migrationdata.migration as m on r.hotel_' at line 1
But a similar select query works for me and gives proper results.
select r.hotel_id, m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
What I am doing wrong in the update query?
The correct MySQL syntax is:
update ratingsapp.hotel03 r inner join
migrationdata.migration as m
on r.hotel_id = m.restaurant_id
set internal_id = m.internal_id ;
There is no from clause in a MySQL update. You are using SQL Server/Postgres syntax.
I try to select the the rows with the newest timestamp in change_date from a table in a LEFT JOIN. I really don't know why this query fails:
SELECT
i.ID, i.title, i.create_date,
u1.username creator_name,
u2.username assignee
FROM item i
LEFT JOIN user u1 ON u1.login_IDFK = i.creator_IDFK
LEFT JOIN user u2 ON u2.login_IDFK = i.assigned_to_IDFK
LEFT JOIN (
SELECT MAX(change_date), item_IDFK FROM item_state GROUP BY item_IDFK
) AS ist ON ist.item_IDFK = i.ID
I get the following 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 ') AS ist ON ist.item_IDFK = i.ID' at line 2 (Code: 1064)
Query works great without the last LEFT JOIN
(SELECT change_date, item_IDFK FROM item_state GROUP BY item_IDFK)
You are using a group by clause without an aggregate. Each item in the select list must either be represented in the group by clause, or be part of an aggregate expression
I.E.
(Select Max(Change_Date), item_IDFK from item_state group by item_IDFK)
try to save your last subquery in a view table, and after that, left join from that table, and see if the syntax error persists.
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