Update and select query in mysql query - mysql

I am using the below format to use select and update command in the same query.
UPDATE t
SET t.col1 = o.col1
FROM table1 AS t
INNER JOIN
table2 AS o
ON t.id = o.id
I applied the same concept in my query but it is throwing an error which I am not able resolve. Any idea where I am going wrong here?
update T set T.price = 2*OT.ingredients from Cake as T Inner join (select
A.cakeid, B.price, sum(C.price) ingredients
from
Contain as A
inner join
Cake as B ON A.cakeid = B.cakeid
inner join
Ingredient as C ON C.ingredid = A.ingredid
group by A.cakeid
having B.price <= 2 * sum(C.price) ) as OT on OT.cakeid = T.cakeid
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 'from Cake as T Inner join (select A.cakeid, B.price, sum(C.price) ingredien' at line 1

The correct syntax in MySQL is:
update Cake T Inner join
(select A.cakeid, B.price, sum(C.price) ingredients
from Contain A inner join
Cake B
ON A.cakeid = B.cakeid inner join
Ingredient as C
ON C.ingredid = A.ingredid
group by A.cakeid
having B.price <= 2 * sum(C.price)
) OT
on OT.cakeid = T.cakeid
set T.price = 2*OT.ingredients ;

Related

Get only rows where exists in table?

I need to select all rows from table: Announcements where there are exist rows in table Categories_announcements for user by condition:
Categories_user.category_id = Categories_announcements.category_id
I tried SQL query, see link
It should returns me onlu one row Announcements.id = 1, because user has only one category in Categories_user.category_id.
EDIT:
I have tested SQL that you shared, so this is query:
select *
from `announcements`
where exists (
select 1
from `announcement_category`
inner join `user_category` on `user_category`.`category_id` = `announcement_category`.`category_id`
where `Auser_category`.`user_id` = 1
and `announcement_category`.`announcement_id` = announcements.id
)
It returns me an error:
#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 ') LIMIT 0, 25' at line 7
I don't think you need an EXISTS subquery. A basic join statement should work.
select a.*
from Categories_user cu
join Categories_announcements ca on ca.category_id = cu.category_id
join Announcements a on a.id = ca.announcement_id
where cu.user_id = 1
You need to correlate the subquery with the Announcements table:
select *
from Announcements a
where exists (
select 1
from Categories_announcements ca
inner join Categories_user cu on cu.category_id = ca.category_id
where cu.user_id = 1
and ca.announcement_id = a.id
)
Demo
You are close. Try this:
select a.*
from Announcements a
where exists (select 1
from Categories_announcements ca join
Categories_user cu
on cu.category_id = ca.category_id
where ca.announcement_id = a.id and
cu.user_id = 1
);
Notes:
The key thing you are missing is the correlation clause to the outer query (ca.announcement_id = a.id).
The left join is superfluous. The where clause turns it into an inner join.
Table aliases make the query easier to write and to read.
You'r query is missing a relationship between the announcement table in the outer query to the condition in the inner one:
SELECT *
FROM announcements a
WHERE EXISTS (SELECT *
FROM categories_announcements ca
LEFT JOIN categories_user cu ON cu.category_id = ca.category_id
WHERE cu.user_id = 1 AND
a.id = ca.announcement_id -- Here!
)
SQLFiddle
Another possible workaround without a sub-query:
SELECT a.id AS accouncement_id, a.name AS annoucement_name
FROM Categories_user cu
INNER JOIN Categories_announcements ca
ON cu.category_id = ca.category_id
AND cu.user_id = 1
INNER JOIN Announcements a
ON ca.announcement_id = a.id;
Demo link

Update a table by joining 3 tables

i have 3 tables:
tblproduct (pro_Id, qty, unitprice)
tblorderdetails (order_id, pro_Id, qty)
tblorder (order_id, totalAmount)
i intend joining these tables to as to update the totalAmount in the tblorder table. This is my query using MySql console:
UPDATE o
SET o.totalAmount = p.unitprice * d.qty
FROM tblorder o INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id;
This is the error i get:
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 'from tblorder o inner join tblorderdetails d
The syntax is not correct and it should be as
UPDATE tblorder o
INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id
SET o.totalAmount = p.unitprice * d.qty ;

Joining two select queries and ordering results

Basically I'm just unsure as to why this query is failing to execute:
(SELECT replies.reply_post, replies.reply_content, replies.reply_date AS d, members.username
FROM (replies) AS a
INNER JOIN members ON replies.reply_by = members.id)
UNION
(SELECT posts.post_id, posts.post_title, posts.post_date AS d, members.username
FROM (posts) as b
WHERE posts.post_set = 0
INNER JOIN members ON posts.post_by = members.id)
ORDER BY d DESC LIMIT 5
I'm getting this 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 'a INNER JOIN members ON replies.re' at line 2
All I'm trying to do is select the 5 most recent rows (dates) from these two tables. I've tried Join, union etc and I've seen numerous queries where people have put another query after the FROM statement and that just makes no logical sense to me as to how that works?
Am I safe to say that you can join the same table from two different but joined queries? Or am I taking completely the wrong approach, because frankly I can't seem see how this query is failing despite reading the error message.
(The two queries on there own work fine)
I think there is syntax error in your query at below part :
FROM (posts) as b
WHERE posts.post_set = 0
INNER JOIN members ON posts.post_by = members.id)
Inner join should come first before where condition. Also your join conditions are wrong. You need to apply conditions like
INNER JOIN members ON a.reply_by = members.id)
INNER JOIN members ON b.post_by = members.id)
So your query should be like this
(SELECT a.reply_post, a.reply_content, a.reply_date AS d, members.username
FROM (replies) AS a
INNER JOIN members ON a.reply_by = members.id)
UNION
(SELECT b.post_id, b.post_title, b.post_date AS d, members.username
FROM (posts) as b
INNER JOIN members ON b.post_by = members.id
WHERE b.post_set = 0)
ORDER BY d DESC LIMIT 5
Try this:
(SELECT a.reply_post, a.reply_content, a.reply_date AS d, members.username
FROM replies AS a
INNER JOIN members ON a.reply_by = members.id)
UNION
(SELECT b.post_id, b.post_title, b.post_date AS d, members.username
FROM posts as b
INNER JOIN members ON b.post_by = members.id
WHERE b.post_set = 0) /* Use where condition after matching Id's using ON */
ORDER BY d DESC LIMIT 5

phpmyadmin update query using 3 tables in giving error

I am getting the following Error
064 - 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 catalog_product_flat_1 a inner join
catalog_product_entity c on a.sku' at line 3
when I try out the below query on phpmyadmin
update a
set a.small_image = b.value
from `catalog_product_flat_1` a
inner join `catalog_product_entity` c
on a.sku = c.sku
inner join `catalog_product_entity_media_gallery` b
on b.entity_id = c.entity_id
I also tryout without using alias but still same issue
The SET clause comes after the JOIN clauses:
UPDATE `catalog_product_flat_1` a
inner join `catalog_product_entity` c
on a.sku = c.sku
inner join `catalog_product_entity_media_gallery` b
on b.entity_id = c.entity_id
SET a.small_image = b.value
See the documenation:
http://dev.mysql.com/doc/refman/5.5/en/update.html

MYSQL query using Left Join and Where IN clause

I have three tables A B C and i'm trying to retrieve information from all three.
A has the columnns userid avatar username and B has the column postid, dateshared and C has the column commenter postid datecommented.
I'm trying to run the query
Select C.comment, C.commenter, C.datecommented, B.postid, B.dateshared A.username A.avatar from B Left Join C Left join A on C.postid = B.postid AND A.userid = C.commenter where B.postid IN ('1','2','3') order by C.dateshared desc
but it gives 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 'where B.postid IN ('1', '2', '3') order by C.dateshared '
Can anyone point out what I'm doing wrong or suggest how to go about it?
Each LEFT JOIN requires its own ON condition:
SELECT C.comment, C.commenter, C.datecommented, B.postid, B.dateshared, A.username A.avatar
FROM B
LEFT JOIN
C
ON C.postid = B.postid
LEFT JOIN
A
ON A.userid = C.commenter
WHERE B.postid IN ('1','2','3')
ORDER BY
C.dateshared desc
This should work for you, your query had some syntax errors:
Select C.comment,C.commenter,C.datecommented,B.postid,B.dateshared,A.username,A.avatar
from B
Left Join C on C.postid = B.postid
Left join A on A.userid = C.commenter
where B.postid IN ('1','2','3')
order by C.dateshared desc