I have two tables : first one stores main informations and the second stores some extra informations. I need to update the first table with the help of some data stored at second table.
My SELECT statement is working
SELECT news.news_id,
news.title,
news.cat_id,
news.sub_cat_id,
news_extra.date_vision_tr
FROM news_extra
JOIN news
ON news.news_id = news_extra.news_id
WHERE news.cat_id=1 AND sub_cat_id=5 AND news_extra.date_vision_tr < CURDATE()
Than I tried UPDATE statement like this
UPDATE news SET news.sub_cat_id=8
FROM news
INNER JOIN news_extra
ON news.news_id = news_extra.news_id
WHERE news.cat_id=1 AND sub_cat_id=5 AND news_extra.date_vision_tr < CURDATE()
But its not working. Giving this 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 news INNER JOIN news_extra ON news.news_id =
news_extra.news_id WHERE news.' at line 2*
The syntax you are using is for SQL Server.
Here is the syntax for MySQL
UPDATE news a
INNER JOIN news_extra b
ON a.news_id = b.news_id
SET a.sub_cat_id = 8
WHERE a.cat_id = 1 and
sub_cat_id = 5 and
b.date_vision_tr < CURDATE()
You must be familiar with SQL Server more. The syntax you are using will work in SQLServer. For MySQL:
UPDATE news
INNER JOIN news_extra
ON news.news_id = news_extra.news_id
SET news.sub_cat_id=8
WHERE news.cat_id=1 and sub_cat_id=5 and news_extra.date_vision_tr < CURDATE()
--Try this way.
Check similar thread
Related
I would like to update with 2 join.... but :
UPDATE glpi.glpi_users
FROM
glpi.glpi_groups_users
INNER JOIN glpi.glpi_groups ON glpi.glpi_groups_users.groups_id = glpi.glpi_groups.id
INNER JOIN glpi.glpi_users ON glpi.glpi_users.id = glpi.glpi_groups_users.users_id
SET glpi.glpi_users.id = 2
WHERE
glpi.glpi_groups.`name` LIKE 'technique'
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 'FROM
glpi.glpi_groups_users
INNER JOIN glpi.glpi_groups ON glpi.glpi_groups_us' at line 2
Time: 0s
thanks for your help
I think that the syntax you want is:
UPDATE glpi.glpi_users u
INNER JOIN glpi.glpi_groups_users gu ON gu.users_id = u.id
INNER JOIN glpi.glpi_groups g ON g.id = gu.groups_id
SET u.id = 2
WHERE g.name = 'technique'
That is: MySQL update/join syntax doesn't have a FROM clause - it looks like UPDATE ... JOIN ... SET ... WHERE ....
Notes:
since no wildcards do appear in the right side operand, name LIKE 'technique' is equivalent to name = 'technique'
table aliases make the query easier to write and read
UPDATE newsreactions
SET newsreactions.enabled = '0'
FROM newsreactions
INNER JOIN users ON newsreactions.memberId = users.id
WHERE users.active = '0' AND users.comment LIKE '%spam%'
For some reason I'm getting a syntax 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 'FROM newsreactions INNER JOIN users ON newsreactions.memberId = users.id WHERE u' at line 3
Can't figure it out though.
If I replace the update and set by a select it works fine.
Error 1064 is a MySQL syntax error. The correct MySQL syntax is:
UPDATE newsreactions nr INNER JOIN
users u
ON nr.memberId = u.id
SET nr.enabled = 0
WHERE u.active = 0 AND u.comment LIKE '%spam%';
Notes:
The JOIN goes in the UPDATE clause.
Table aliases makes the query easier to write and to read.
I am guessing that enabled and active are really numeric values. If so, do not use single quotes.
The join clause should come before the set clause, and there should be no from clause in MySQL:
UPDATE newsreactions
JOIN users ON newsreactions.memberId = users.id
SET newsreactions.enabled = '0'
WHERE users.active = '0' AND users.comment LIKE '%spam%'
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
When trying to perform this query:
>mysql_query("UPDATE contracts
SET x = '1'
FROM contracts
INNER JOIN employees
ON contracts.contract_employeeid=employees.employee_id
WHERE experience >= '6'") or die(mysql_error());
I get the following error message:
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 contracts INNER JOIN employees ON contracts.contract_employeeid=employees.employee_id WHERE experience >= '6'
In words, I need to set x=1 on the table "contracts" for the employees who have more than 6 years of experience (to do so, I need to join with the table "employees" on employee_id=contract_employeeid since their experience is stored in that table)
You can probably move the JOIN part of your query before the SET statements:
UPDATE contracts
INNER JOIN employees ON contracts.contract_employeeid = employees.employee_id
SET x = '1'
WHERE experience >= '6'
UPDATE contracts, employees
SET contracts.x = '1'
WHERE contracts.contract_employeeid=employees.employee_id
AND employees.experience >= '6'
UPDATE table1 SET announcer = ( SELECT memberid
FROM ( table1
JOIN users ON table2.username = table1.announcer
) AS a
WHERE a.username = table1.announcer )
#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 'a where
a.username=table1.announcer)'
at line 1
Try:
UPDATE announcements a
SET announcer =
(SELECT memberid
FROM users u
WHERE u.username = a.announcer)
You can also do the JOIN in the UPDATE
UPDATE announcements JOIN users
SET announcements.announcer=users.memberid
WHERE announcements.username=users.username;
Note: For safty reasons (until your sure announcers get copied over right) I'd instead create a new column, say announcerNew then
UPDATE announcements JOIN users
SET announcements.announcerNew=users.memberid
WHERE announcements.username=users.username;