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
Related
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 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 have following query that is working fine in TSQL
SELECT
shoppingcart_1.price, shoppingcart_1.stid, course.isbn,
book.BookTitle, course.Course_ID, schedule.stid AS Expr1
FROM
book
INNER JOIN
shoppingcart AS shoppingcart_1
INNER JOIN
schedule ON shoppingcart_1.cid = schedule.course_ID
INNER JOIN
course ON schedule.course_ID = course.Course_ID
ON book.isbn = course.isbn
WHERE
(shoppingcart_1.stid = '20070004')
but when I run it in Mysql it displays error on line
INNER JOIN course ON schedule.course_ID = course.Course_ID ON book.isbn = course.isbn
Error text is
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 'ON book.isbn = course.isbn WHERE (shoppingcart_1.stid =
'20070004') LIMIT 0' at line 6
I am writing queries first time in mysql, please help
Seems like you have your joins a little mixed up. I tried cleaning it up. See if this works for you
SELECT shoppingcart_1.price, shoppingcart_1.stid, course.isbn, book.BookTitle, course.Course_ID, schedule.stid AS Expr1
FROM book
INNER JOIN course ON book.isbn = course.isbn
INNER JOIN schedule ON course.Course_ID = schedule.course_ID
INNER JOIN shoppingcart AS shoppingcart_1 ON schedule.course_ID = shoppingcart_1.cid
WHERE shoppingcart_1.stid = '20070004'
Note how I join book to course, then course to schedule, then schedule to shoppingcart. Then use the WHERE clause to specify other conditions.
I am running a query to delete from table.
delete from sps
inner join str on str.studentid = sps.studentid
where str.studentid like '%2012%psy%'
and str.semesterid=2
inner join papers on papers.id = sps.paperid
where papers.c_id=10
and papers.p_semid=2
I don't know why it is not showing any result and giving me an error.
The error is :
#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
'inner join str on str.studentid=sps.studentid where str.studentid like '%2012%ps'
at line 2
Move the two predicates of the two WHERE clauses into one WHERE clause at the end of your query, as described by the Mysql DELETE syntax. Something like:
DELETE s
FROM sps s
INNER JOIN str ON str.studentid = s.studentid
INNER JOIN papers ON papers.id = s.paperid
WHERE str.studentid LIKE '%2012%psy%'
AND str.semesterid = 2
AND papers.c_id = 10
AND papers.p_semid = 2
You can use WHERE clause once in a single query.
Try this query
DELETE sps FROM sps
INNER JOIN str ON str.studentid=sps.studentid
INNER JOIN papers ON papers.id=sps.paperid
WHERE papers.c_id=10 AND papers.p_semid=2
AND str.studentid LIKE '%2012%psy%' AND str.semesterid=2
OR
DELETE FROM sps, str, papers
USING sps
INNER JOIN str ON str.studentid=sps.studentid
INNER JOIN papers ON papers.id=sps.paperid
WHERE papers.c_id=10
AND papers.p_semid=2
AND str.studentid LIKE '%2012%psy%'
AND str.semesterid=2
I have the following query :
select irc.*,p.*,#product :='prod_product',#accessrole :='pub_accessrole'
from item_rel_coupon irc
join user_rel_coupon urc on urc.userId = 7 and irc.couponId=urc.couponId
left join if(irc.source='product',#product,#accessrole) as p on p.id=irc.itemId
But I get a syntax error. Why?
Nanne is right, your IF() is not a table. There are two ways around:
You join on both tables, and put the
IF in your select to select the
columns from the table you want.
(recommended)
You use the IF to create the query
in a string, you PREPARE the string
and EXECUTE the handle. (not
recommended)
This part gives an error:1
left join if(irc.source='product',#product,#accessrole) as p on p.id=irc.itemId
I after a JOIN you need a table reference, and I don't think your if 's result is one.
1: the 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 'if(irc.source='product',#product,#accessrole) as p on p.id=irc.itemId LIMIT 0,' at line 1
I don't think I've ever seen an IF() used as a table reference in a query, especially if the IRC source can alternate between different sources... I would change to...
select
irc.*,
if( p1.id = irc.itemid, p1.fld1, p2.fld1 ) as Fld1,
if( p1.id = irc.itemid, p1.fld2, p2.fld2 ) as Fld2,
if( p1.id = irc.itemid, p1.fld3, p2.fld3 ) as Fld3,
if( p1.id = irc.itemid, p1.fld4, p2.fld4 ) as Fld4
from
item_rel_coupon irc
join user_rel_coupon urc
on urc.userId = 7
and irc.couponId=urc.couponId
left join prod_product p1
on p1.id = irc.itemid
left join pub_accessrole p2
on p2.id = irc.itemid