I am using MYSQL version 5.5
I am trying to insert the following procedure:
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
(INSERT INTO `tbl_search_counter` (`user_id` ,`time_searched`) VALUES ('165', '7'));
END IF
But for whatever reason I get the following error message
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 'INSERT INTO tbl_search_counter (user_id ,time_searched)
VALUE' at line 6
I really don't understand because when I do the following request I do not experience problem
CREATE PROCEDURE `myTestProceed2ure` ( IN `_id` INT )
INSERT INTO `tbl_search_counter` (`user_id`, `time_searched`) VALUES ('15', '7')
or the following
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
END IF
If someone has any idea what I am doing wrong, let me know...
Thanks
What is your stored procedure supposed to be doing? You are not referencing the parameter to the stored procedure and u_userid seems undefined.
If you just want to insert an id that doesn't currently exist, then here is one way:
CREATE PROCEDURE `myTestProceed3ure` (IN _id INT )
INSERT INTO tbl_search_counter(user_id, time_searched)
VALUES (_id, '7')
ON DUPLICATE KEY UPDATE user_id = values(user_id)
END IF;
Final answer: I should not have put () before the INSERT...
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
INSERT INTO `tbl_search_counter` (`user_id` ,`time_searched`) VALUES ('165', '7');
END IF
Related
I am currently using the SQL command
Select * from where name='john'
Is it possible to return 20 no matter the query, for example
Select * from where name='john' or return = 20
EDIT
If you have an oracle database you can do something like that:
SELECT *
FROM dual
WHERE 1=0
UNION
SELECT '20'
FROM dual;
check my answer
if exists (Select * from item where ItemName='ABC Daycare1')
begin
Select * from item where ItemName='ABC Daycare1'
end
else
select '20'
Try running this. This should return the top result (which is never 20 due to the custom sort) and then when the name doesn't match a value it returns 'Mark' and 20
SQL
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT TOP 1
*
FROM #temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
MySQL - MySQL fiddle
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT
*
FROM temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
LIMIT 1
select * from csclass where cnumber not in (
(select distinct cnumber from temp where taken=0) union (select cnumber from taken where username = "1")
);
This is what i have now. I just google it and realized that not in could not be a table. So how could i do this?
Thank you.
The below is my whole code.
create temporary table temp
(cnumber VARCHAR(45) not null
, lclass VARCHAR(45) not null
,taken boolean not null default false
);
insert into temp(cnumber,lclass)
select uclass,lclass from pre;
update temp,taken set temp.taken=true where temp.lclass=taken.cnumber;
select * from csclass where cnumber not in (
(select distinct cnumber from temp where taken=0) union(select cnumber from taken where username = "1")
);
So sorry for my bad English skill. So when i run this, there is error message: Error Code 1064. Syntax error.
So in the end i fixed this with below code:
select * from csclass where cnumber not in ( select cnumber from(
(select distinct cnumber from temp where taken=0) union (select cnumber from taken where username = "1")
) as total) ;
And Thank you guys.
I have the following SQL update trigger that works properly:
BEGIN
DECLARE myID INT;
SELECT user_id INTO myID FROM writer WHERE writer_id = NEW.writer_id;
IF (NEW.status_id = 2) THEN
INSERT INTO activity (
user_id,
work_id,
activity,
date_created
) VALUES (
myID,
NEW.work_id,
'confirmed',
now()
);
ELSE
INSERT INTO activity (
user_id,
work_id,
activity,
date_created
) VALUES (
myID,
NEW.work_id,
'modified',
now()
);
END IF;
END
I need to add an additional trigger as the following:
CREATE TRIGGER updateWorkStatus AFTER UPDATE ON writer_split
FOR EACH ROW
BEGIN
UPDATE work a
JOIN writer_split b
ON a.work_id = b.work_id AND a.current_version = b.version
SET a.status_id = 2
WHERE a.work_id NOT IN (
SELECT ab.work_id
FROM (SELECT s.work_id
FROM work w INNER JOIN writer_split s
ON w.work_id = s.work_id AND s.status_id != 2) ab
);
END;
when I run this create script, I am getting a syntax error. Any ideas?
For me i will use UPDATE work as a
I have a query with "SELECT CASE" statement that's working fine:
SELECT
(CASE `t`.`is_combined`
WHEN 0
THEN `t`.`topic_id`
ELSE `t`.`is_combined`
END) AS`group_id`,
SUM(`ctt`.`tm_download_status`) AS `is_downloaded`,
COUNT(`t`.`topic_id`) AS `group_topics_cnt`,
(SUM(`ctt`.`tm_download_status`) = COUNT(`t`.`topic_id`)) AS `is_downloaded_group`
FROM (`catalog_topics` `t` LEFT JOIN `catalog_tracker_torrents` `ctt` ON((`ctt`.`topic_id` = `t`.`topic_id`)))
WHERE (`t`.`topic_id` != 0)
GROUP BY (`group_id`)
So, i want to create a similar trigger to update "cross" table:
DELIMITER $$
CREATE TRIGGER `tdg_ins_by_topics` AFTER INSERT ON `catalog_topics` FOR EACH ROW
BEGIN
REPLACE INTO catalog_topics_downloaded_groups(
SELECT (
CASE `t`.`is_combined`
WHEN 0
THEN `t`.`topic_id`
ELSE `t`.`is_combined`
END
) AS `group_id` ,
SUM( `ctt`.`tm_download_status` ) AS `is_downloaded` ,
COUNT( `t`.`topic_id` ) AS `group_topics_cnt` , (
SUM( `ctt`.`tm_download_status` ) = COUNT( `t`.`topic_id` ) ) AS `is_downloaded_group`
FROM `catalog_topics` `t`
LEFT JOIN `catalog_tracker_torrents` `ctt` ON `ctt`.`topic_id` = `t`.`topic_id`
WHERE `t`.`topic_id`
IN (
NEW.`topic_id`
)
GROUP BY `group_id`
)
END ;
$$
But getting an error message:
"#"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 'END' at line 14
It's look like that MySQL doesn't understand difference between CASE in TRIGGER statement and CASE in SELECT statement. So, how i can fix this?
Thanks for the answers.
I think you need to "end" your REPLACE statement with ; like you have to end all statements inside a TRIGGER or PROCEDURE/FUNCTION with a delimeter.
That is why you change the DELIMETER to $$ .. so you can use ; to store the mysql default delimeter inside of the trigger code. (and end the create trigger statement with the changed $$ delimeter)
DELIMITER $$
CREATE TRIGGER `tdg_ins_by_topics` AFTER INSERT ON `catalog_topics` FOR EACH ROW
BEGIN
REPLACE INTO catalog_topics_downloaded_groups(
SELECT ( CASE `t`.`is_combined`
WHEN 0
THEN `t`.`topic_id`
ELSE `t`.`is_combined`
END
) AS `group_id`,
SUM(`ctt`.`tm_download_status`) AS `is_downloaded`,
COUNT( `t`.`topic_id` ) AS `group_topics_cnt` ,
(
SUM( `ctt`.`tm_download_status` ) = COUNT( `t`.`topic_id` ) ) AS `is_downloaded_group`
FROM `catalog_topics` `t`
LEFT JOIN `catalog_tracker_torrents` `ctt` ON `ctt`.`topic_id` = `t`.`topic_id`
WHERE `t`.`topic_id` IN ( NEW.`topic_id` )
GROUP BY `group_id`
);
END;
$$
DELIMETER ;
Table looks like this:
create table #rankme (rankmeid int identity(1000, 1) primary key,
step int null, checkvalue int null)
insert into #rankme values ( 10 , 1 )
insert into #rankme values ( 15 , null )
insert into #rankme values ( 20 , null )
insert into #rankme values ( 40 , null )
select * from #rankme order by step,checkvalue
Taking step as a parameter, I am trying to find out if the requested checkvalue for the one before the step I asked for is null.
So I want to select where step=20 and get NULL.
And I want to select where step=15 and get a 1.
I was trying to come up with something based on "rank-1" but so far no cigar.
Help?
declare #step int = 15
select top(1) R.checkvalue
from #rankme as R
where R.step < #step
order by R.step desc