How do I do a DELETE in MySQL with NOT IN? [duplicate] - mysql

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 7 years ago.
I'm getting this error when running the code:
Error Code: 1093. You can't specify target table 'details' for update
in FROM clause
DELETE FROM details WHERE detail NOT IN
(
SELECT detail
FROM user_details
JOIN data ON user_details.data_iddata = data.iddata
JOIN details ON details.iddetails = data.details_iddetails
)
What am I doing wrong here?

I don't have a system handy, but I think the problem is that you can reference the same table in a subquery as the main one you are deleting from. I.e. it doesn't like JOIN details ON details.iddetails = data.details_iddetails in the subquery because that's in the main select clause.
One workaround is to create a temp table, insert the records you are interested in into it, and then find your set from a join off of that.

You're trying to do a DELETE from a table while you're doing a SELECT from it.
In order to get this to work, make a list from your SELECT query and put its results in the parentheses.
If you aren't doing this in an external script, you should define a view that will give you the desired SELECT output.

Related

I am getting an error in mysql update query using inner join [duplicate]

This question already has answers here:
How can I do three table JOINs in an UPDATE query?
(7 answers)
Closed 3 years ago.
I have got an error in running this query in phpMyadmin. The query is to update a table using inner join .Pls help me
My tables are :
userpswd
ID
password
Name
Col 4
Col 5
index
oncampus_present ( the colum I want to update )
oncampus
index
ID
I already have gone through most of the threads given here regarding the same and none seem to work.
UPDATE userpswd
SET oncampus_present='yes'
FROM userpswd
INNER JOIN oncampus
ON userpswd.ID=oncampus.ID
WHERE userpswd.oncampus_present=NULL
You have an error in your SQL syntax
Try This
UPDATE userpswd
INNER JOIN oncampus
ON userpswd.ID=oncampus.ID
SET oncampus_present='yes'
WHERE userpswd.oncampus_present IS NULL

Can't update existing records MySQL [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 4 years ago.
I've created a new column (third one) and all records are null in that column. The table has 100 rows. When I try INSERT INTO, records are added but only from 101 row and previous ones are still null. That's why I decided to use UPDATE, but I have the next error message: You can't specify target table 'actors' for update in FROM clause
Here is my query:
UPDATE
actors
SET
starred_count = (
SELECT COUNT(actors.Actor_id)
FROM starred
INNER JOIN actors ON actors.Actor_id = starred.Actor_id
GROUP BY starred.Actor_id
ORDER BY starred.Actor_id
)
How could I do this properly?
Give a try to next query, since on MySQL updating using a query is not valid.
UPDATE
actors AS a
INNER JOIN
(SELECT Actor_id, COUNT(*) AS starred_count
FROM starred
GROUP BY Actor_id) AS s ON s.Actor_id = a.Actor_id
SET
a.starred_count = s.starred_count

Incorrect SQL Syntax when using JOIN [duplicate]

This question already has an answer here:
MySQL Inner Join Query Syntax error
(1 answer)
Closed 4 years ago.
In my DB I have 2 Tables. One for user Login information and one for general information.
Im trying to write a query that will select the column "firstname" from rows where the FK "users_id" is the same as the logged in users ID.
Before doing anything in PHP Im running the query in my database, so the logged in users ID, which would normally be a variable, is replaced with the id of my testuser.
This is my query:
SELECT b6vjp_user_info.firstname
FROM b6vjp_user_info
WHERE b6vjp_user_info.users_id LIKE 243
INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id;
And here is my (censored for security reasons) Login Table named "b6vjp_users":
And here is my other table named "b6vjp_user_info":
The error is:
#1064 - Mistake in SQL-Syntax. 'INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id LIMIT 0, 25' on row 4
Now fyi I translated that, because my work environment is in german. But im sure you know what a Syntax-Error is.
Anyways I checked the JOIN Part of my query over and over again and looked up the JOIN tutorial on W3Schools. But there is no apparent mistake.
Does anybody see what I somehow fail to?
Put the WHERE clause after the (last) ON clause.
I strongly recommend using table aliases. You also need to fix the order of your SQL clauses. So:
SELECT ui.firstname
FROM b6vjp_user_info ui JOIN
b6vjp_users u
ON ui.users_id = u.id
WHERE ui.users_id = '243';
Or, more simply without the JOIN:
SELECT ui.firstname
FROM b6vjp_user_info ui
WHERE ui.users_id = '243';
Notes:
The operand to LIKE should be a string. So, make it a string! Implicit type conversion causes all sorts of problems.
If you are not using wildcards, I think = is more informative. If users_id is really a number, then just use = 243 rather than LIKE.
WHERE goes after the FROM clause. JOIN is an operator in the FROM clause.
The JOIN is not necessary. Unless you are fetching columns from the users table (or need it for filtering which is highly doubtful), don't bother with it.
you have to put where after on clause
SELECT b6vjp_user_info.firstname
FROM b6vjp_user_info
INNER JOIN
b6vjp_users ON
b6vjp_user_info.users_id=b6vjp_users.id
WHERE b6vjp_user_info.users_id =243 // i think it int field so no need to use like operator

MySQL Error 1054 Unknown Column 'persons.PersonID' in on clause [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 6 years ago.
Here i am trying to run one query in MySQL editor and getting one problem
Here is my query
select *
FROM my_db.persons FULL JOIN
my_db.employee
ON persons.PersonID=employee.PersonID;
Any help would be appreciated
MySQL doesn't support FULL JOIN, so perhaps that is the problem. In any case, I prefer shorter table aliases:
select *
FROM my_db.persons p LEFT JOIN
my_db.employee e
ON p.PersonID = e.PersonID;
This, of course, assumes that the PersonID column exists in both tables.
Oh, I see why you got the error. Perhaps this will explain:
select *
FROM my_db.persons full JOIN
my_db.employee e
ON full.PersonID = e.PersonID;
That is, because MySQL doesn't support FULL JOIN, the full is treated as a table alias.
Check if PersonID column exists on Persons table. Make sure the spellings are exactly the same as in the table structure. Also check the case. Some IDE are case sensitive.

Update entire table based on parameter of another table in a matching field [duplicate]

This question already has answers here:
MySQL syntax for Join Update
(2 answers)
Closed 8 years ago.
I've my table user_data that has over 8k records. Then I have my table Geo_location. Both tables join on the member_num column
Geo_location has a column called public that should match the table user_data column acceptinClients. Its an INT 0 or 1
I am having some data consistency problem and not all record match. I know how to do a long winded fetch and do one by one update in PHP if they dont match.
But I was wondering if there is a way to create a SQL query that will loop through all records in Geo_location - public and update them with whatever value is in user_data - acceptinClients
Is there a way to do that or am i asking too much? if so, how?
i have been reading around but cant find a simple solution for my problem or dont understand them
I don't know if I understand the problem, but isn't
UPDATE Geo_location AS g
JOIN users_data AS u ON u.member_num = g.member_num
SET g.public = ud.acceptinClients
correct?