Is posible to using in paramenter in subquery?
I'm trying to join three table, and using paramenter in subquery, but I'm getting error:
drop procedure if exists displayFilmInfo;
delimiter //
create procedure displayFilmInfo(in in_category_id tinyint, in in_language_id tinyint)
begin
if in_category_id != 0 and in_language_id != 0 then
select film.title, after_category.name from film
inner join film_category on film_category.film_id = film.film_id
inner join (
select category_id, name from category when category.category_id = in_category_id
) after_category on after_category.category_id = film_category.category_id
inner join (
select language_id, name from language when in_language_id = language.language_id
) after_language on after_language.language_id = film.language_id
end if;
end //
delimiter ;
call displayFilmInfo();
It show:
Query OK, 0 rows affected, 1 warning (0.00 sec)
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 'when category.category_id = in_category_id
) after_category on afte' at line 8 ERROR 1305 (42000): PROCEDURE sakila.displayFilmInfo does not exist
Please update your select statement to -
select film.title,
after_category.name
from film
inner join film_category on film_category.film_id = film.film_id
inner join (select category_id,
name
from category
where category.category_id = in_category_id) after_category on after_category.category_id = film_category.category_id
inner join (select language_id,
name
from language
where in_language_id = language.language_id) after_language on after_language.language_id = film.language_id
Related
select Customer.CustNo, Customer.FirstName, Customer.LastName,
`Order`.OrderNo, `Order`.OrderDate, Employee.EmpNo,
Employee.FirstName, Employee,LastName, Product.ProdNo,
Product.ProdName,
CAST(substring(Product.Price,2) AS FLOAT) * ProductInOrder.Qty AS 'OrderCost'
From `Order`
inner join Employee on `Order`.Employee_EmpNo = Employee.EmpNo
inner join Customer on Customer.CustNo = `Order`Customer_CustNo
inner join ProductInOrder on `Order`.OrderNo = ProductInOder.Order_OrderNo
inner join Product on ProductInOrder.Product_ProdNo = Product.ProductNo
Where Employee.EmpNo is not null
and str_to_date(`Order`.OrderDate,'%Y-%m-%d') = '2007-1-23'
Having OrderCost > 150
Order by OrderCost Desc
After I execute it the 'ER_PARSE_ERROR' error appear.
Full error message:
ER_PARSE_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 'FLOAT) * ProductInOrder.Qty AS 'OrderCost' From Order inner join Employee on `' at line 1'
I want to use data of view in WHERE clause. But getting an error:
create view post_with_answers AS
SELECT DISTINCT postid
FROM (SELECT postid FROM `qa_posts` WHERE `type` = 'Q') AS q1
INNER JOIN (SELECT parentid FROM `qa_posts` WHERE `type` = 'A') AS q2 ON q1.postid = q2.parentid
select count(*)
from qa_posts
where parentid not in post_with_answers
On the last row I am getting this error:
SQL 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 'post_with_answers' at line 3
How to fix that?
Just like you would use a table:
select count(*)
from qa_posts
where parentid not in (select pwa.postid from post_with_answers pwa);
I would caution you from using not in with a subquery. No rows are returned if even one value from the subquery is NULL. For this reason, I recommend NOT EXISTS:
select count(*)
from qa_posts p
where not exists (select 1
from post_with_answers pwa
where p.parentid = pwa.postid
);
In addition, your view definition is a bit over complicated. You don't need subqueries:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq JOIN
qa_posts pa
ON pq.postid = pa.parentid
WHERE pq.type = 'Q' AND pa.type = 'A';
Then, the DISTINCT just adds overhead, so EXISTS is better:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq
WHERE EXISTS (SELECT 1
FROM qa_posts pa
WHERE pq.postid = pa.parentid AND
pa.type = 'A'
)
WHERE pq.type = 'Q';
I have three tables ( restaurants - votes - comments) . I want to create a trigger which let me update two fields in restaurants ( restaurant_rate_average , restaurant_comment_count ) . Here is my trigger :
DELIMITER $$
CREATE
TRIGGER `change comment status` AFTER UPDATE ON `comments`
FOR EACH ROW
BEGIN
UPDATE restaurants
INNER JOIN votes ON restaurants.restaurant_id = votes.vote_foreign_key
SET restaurants.restaurant_comment_count = SELECT (COUNT(votes.vote_id))
WHERE votes.vote_model = 'restaurant'
AND WHERE old.comment_status<>NEW.comment_status;
UPDATE restaurants
INNER JOIN votes ON restaurants.restaurant_id = votes.vote_foreign_key
SET restaurants.restaurant_rate_average = SELECT (AVG(votes.vote_num))
WHERE votes.vote_model = 'restaurant'
AND WHERE old.comment_status<>NEW.comment_status;
END;
$$
DELIMITER ;
Actually I have 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 'SELECT (COUNT(votes.vote_id))
WHERE votes.vote_model = 'restaurant'
' at line 8
I've tried to make complexion less and replace SELECT (COUNT(votes.vote_id)) with 2 and I get new 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 'WHERE comments.comment_status<>NEW.comment_status;
UPDATE resta' at line 9
How to resolve it?
EDITED :
here is my pseudo tables :
restaurants:
restaurant_id, restaurant_comment_count(int) , restaurant_rate_average(int)
comments:
comment_id , comment_status(boolean) , comment_content(varchar) , votes_vote_id(id)
votes:
vote_id , vote_foreign_key(id), vote_model(varchar)
EDITED 2 :
I create new simple trigger which just join three tables and it works like charm:
BEGIN
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET res.restaurant_rate_average = 2
WHERE cm.comment_status = NEW.comment_status;
END
But if I replace SET res.restaurant_rate_average = 2 with SELECT AVG(v.vote_id) I got an error. I also edit last trigger to :
BEGIN
DECLARE RESTAURANT_RATE_AVERAGE INTEGER;
SET RESTAURANT_RATE_AVERAGE := (SELECT AVG(votes.vote_num)
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_rate_average = RESTAURANT_RATE_AVERAGE
WHERE cm.comment_status = NEW.comment_status;
END
I got an error in line 4. All I want is replace static number with SELECT AVG(votes.vote_num)
DROP TRIGGER IF EXISTS `update_comment_status`;CREATE DEFINER=`root`#`localhost` TRIGGER `update_comment_status` AFTER UPDATE ON `comments` FOR EACH ROW BEGIN
DECLARE RESTAURANT_RATE_AVERAGE INTEGER;
DECLARE RESTAURANT_COMMENT_COUNT INTEGER;
SET RESTAURANT_RATE_AVERAGE := (SELECT AVG(votes.vote_num) FROM votes);
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_rate_average = RESTAURANT_RATE_AVERAGE
WHERE cm.comment_status = NEW.comment_status;
SET RESTAURANT_COMMENT_COUNT := (SELECT COUNT(votes.vote_id) FROM votes);
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_comment_count = RESTAURANT_COMMENT_COUNT
WHERE cm.comment_status = NEW.comment_status;
END
i have 3 tables.
tenant
tenant_id : int
category_id : int
category
category_id : int
category_name : varchar(50)
history
tenant_id : int
bulan_tahun : varchar(8)
counter : int
i want to join all of this table using this code:
SELECT a.tenant_id, a.category_name
FROM (
(tenant INNER JOIN category ON tenant.category_id = category.category_id) AS a
INNER JOIN (
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
) AS b
on a.tenant_id = b.tenant_id
)
but this code produce an 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 ( SELECT tenant_id, counter FROM history WHERE bulan_tahun ' at line 3
if i separate the sub select, it works perfectly
for the first select:
SELECT tenant_id, category_name
FROM
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
and the second select:
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
but if i join this together, the error occured
can anyone help me?
Problem lies here..
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
Try restructuring your entire query like this
SELECT a.tenant_id, c.category_name
FROM
tenant t
INNER JOIN
category c
ON ( t.category_id = c.category_id )
INNER JOIN
history h
ON ( t.tenant_id = h.tenant_id )
WHERE
h.bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
No Idea why this is not working - could someone please help?
update c set c.images = ""
from j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like "cakes%"
and c.created_by in (62,63,99)
and rc.email = 'email'
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 j17_content c inner join j17_jreviews_content rc on rc.contentid = c.id in' at line 2
UPDATE:
Now trying
UPDATE j17_content c SET c.images=''
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
still getting
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 'inner join j17_jreviews_content rc on rc.contentid = c.id inner join j17_catego' at line 2
UPDATE doesn't take a FROM.
The syntax should be UPDATE j17_content c SET c.images="" INNER JOIN ...
This is essentailly your code.
Try it this way:
update j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
set c.images = '';
This is an UPDATE JOIN
I have a refactored version of the query in mind
update
(
SELECT content_id id
FROM j17_jreviews_content
WHERE email = 'email'
) rc
inner join j17_content c USING (id)
inner join
(
SELECT id catid
FROM j17_categories
WHERE created_by in (62,63,99)
AND path like 'cakes%'
) cat USING (catid)
set c.images = '';
You will need indexes for the subqueries involved
ALTER TABLE j17_categories ADD INDEX created_by_path_id_ndx (created_by,path,id);
ALTER TABLE j17_jreviews_content ADD INDEX email_content_id_ndx (email,content_id);
replace your double quotes(") with single ones(')
Sorry this is not really the way you guys suggested - but I got a list of IDs and updated them using no joins with an in statement of all the IDs