MySQL - phpmyadmin - swap columns - mysql

I try swap 3 columns in my table. I try this :
DELIMITER $$
CREATE PROCEDURE px()
BEGIN
DECLARE temp VARCHAR(20);
update `idsaccess` set
temp = referer,
referer = size_var,
size_var = agent,
agent = temp
WHERE agent like '%210%' ;
END $$
CALL p
It don't work. It give me that error: Unknown column 'temp' in 'field list' I do not understand that: temp is varchar value not a column. I also try remove DECLARE and PROCEDURE and just set variable with #. Like this:
set #temp = '';
update `idsaccess`
set #temp = referer,
referer = size_var,
size_var = agent
agent = #temp
WHERE agent like '%210%'
It don't work either. It give me. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax. Any idea what is wrong in my code ? And to avoid misunderstanding I don't want move columns. I just want swap SOME rows (WHERE agent like '%210%') from one column to another.

Try this after taking a backup... I tried for table I have with 2 columns and worked
UPDATE idsaccess SET referer=#tmp:=referer, referer=size_var, size_var = agent, agent = #tmp WHERE agent like '%210%';

Related

trigger to edit other table in mysql

I have the following trigger:
DELIMITER $$
DROP TRIGGER IF EXISTS trg_day_1_status_update$$
USE `tbl_user_status`$$
CREATE DEFINER = CURRENT_USER TRIGGER `tbl_user_status`.`trg_day_1_status_update` AFTER INSERT ON `tbl_tf_day_1` FOR EACH ROW
BEGIN
UPDATE tbl_user_status
SET NEW.roadmap_day = tbl_tf_day_1.roadmap_day,
SET NEW.user_status = "active",
SET NEW.latest_submit = tbl_tf_day_1.submitted_on,
SET NEW.latest_tf_id = tbl_tf_day_1.tf_id,
SET NEW.d0 = 1,
SET NEW.latest_cig_intake = tbl_tf_day_1.q_id_3_ftnd,
SET NEW.latest_cigintake_submit = tbl_tf_day_1.submitted_on
WHERE id_user = tbl_tf_day_1.id_user LIMIT 1;
END;
$$
DELIMITER ;
But I keep getting an 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 'SET NEW.user_status = "active",
SET NEW.latest_submit = tbl_tf_day_1.submitted' at line 5
I've tried a bunch of stuff, including removing the 'NEW' before the fields, changing the value to be inserted, and even making this trigger BEFORE the insert. Can't figure out what's wrong, please help!
A few things wrong here, set only needs to be stated once, the direction of the set statements is incorrect , the column to the left of the equals (=) should the the column you wish to set in the table you wish to update and the column to the right of the equals (=) should the the column you wish to update it from pre- ceeded by a NEW. qualifier (but not for constants such as 'active', the where clause column name to the right of the equals (=) should be preceeded by NEW. I am also a bit dubious about the table name you are trying to update which seeems to share a name with the db/schema in your create statement.
You only need to use the SET keyword once, then comma-separate the fields, like this:
UPDATE tbl_user_status SET
NEW.roadmap_day = tbl_tf_day_1.roadmap_day,
NEW.user_status = "active",
NEW.latest_submit = tbl_tf_day_1.submitted_on,
NEW.latest_tf_id = tbl_tf_day_1.tf_id,
NEW.d0 = 1,
NEW.latest_cig_intake = tbl_tf_day_1.q_id_3_ftnd,
NEW.latest_cigintake_submit = tbl_tf_day_1.submitted_on
WHERE id_user = tbl_tf_day_1.id_user LIMIT 1;

Paramaterised stored procedure in SQL Server - error "Ambiguous column name 'musictypeID'"

I have been working on paramaterised stored procedure in SQL Server, but I get an error stating
Ambiguous column name 'musictypeID'
Code:
create procedure getmusicbytype
(#musictypeID int)
as
begin
select *
from musicc
inner join MusicType on musicc.musictypeID = MusicType.musictypeID
where musictypeID = #musictypeID
end
Both tables have musictypeID, so you need to specify one in the where clause.
Change where musictypeID = #musictypeID to where musicc.musictypeID = #musictypeID

MySQL Syntax : "the right syntax to use near" - right in the beginning

I am a MySQL-noob and today I tried to setup a MySQL call which is more than 5 lines long. I keep getting syntax errors which I try to fix for hours, but I don't have a clue what the problem is. Here is the code:
USE myDatabase;
DELIMITER //
CREATE PROCEDURE MYPROC()
BEGIN
SET #ID = 1;
SET #maxID = 3;
CREATE TEMPORARY TABLE resultTable(v DOUBLE, ttc DOUBLE);
WHILE (#ID < #maxID) DO
INSERT partTable1.v, partTable2.ttc
INTO
resultTable
FROM
(SELECT * FROM
(((SELECT time_sec, v FROM speedTable WHERE (trip_id = #ID)) as partTable1)
INNER JOIN
((SELECT time_sec, ttc FROM sightsTable WHERE (trip_id = #ID)) as partTable2) ON
(0.04 > abs(partTable1.time_sec - partTable2.time_sec)))
);
SET #ID := #ID + 1;
END WHILE;
END //
DELIMITER;
CALL MYPROC();
SELECT * FROM resultTable LIMIT 100;
Is there anything obvious that needs to be corrected?
Update1: Added semicolon to the "CREATE.."-statement, now first three statements are OK.
Update2: Added 3 more semicolons!
Update3: Followed the suggestion to make it a function + separate function call. Error message changed!
Update4: I fixed the issues mentioned in the two answers. Still something wrong there. See updated code above and error message below.
Updated error message:
ERROR 1064 (42000) at line 4: You have an error in your SQL syntax; check the ma
nual that corresponds to your MySQL server version for the right syntax to use n
ear ' partTable2.ttc
INTO
resultTable
FROM
(SELECT * FROM
(((SELE' at line 11
Kind Regards,
Theo
Flow control statements, of which WHILE is one, can only be used within a stored procedure, but you are attempting to use it as a plain query via the console.
If you absolutely must take this path (using mysql instead of an application language), create a store procedure with the code you want, then call it.
Creating the procedure would look like this:
DELIMITER //
CREATE PROCEDURE MYPROC()
BEGIN
WHILE (#ID < #maxID) DO
SET #partTable1 = (SELECT time_sec, v FROM speedTable WHERE (trip_id = #ID));
SET #partTable2 = (SELECT time_sec, ttc FROM sightsTable WHERE (trip_id = #ID));
INSERT v, ttc INTO resultTable FROM
(#partTable1 INNER JOIN #partTable2 ON
(0.04 > abs(partTable1.time_sec - partTable2.time_sec)));
SET #ID := #ID + 1;
END WHILE;
END//
DELIMITER ;
Then to call it:
CALL MYPROC();
See this SQLFiddle of a simplified version of this working.
Note that you do have one syntax error:
#ID = #ID + 1; -- incorrect syntax
SET #ID := #ID + 1; -- correct
Still some syntactic problems and functionality problems...
You can't use WHILE in SQL scripts. You can use WHILE only in the body of a stored routine. See http://dev.mysql.com/doc/refman/5.6/en/flow-control-statements.html
You can't use SET to assign multiple columns to a scalar. MySQL doesn't support relation-valued variables, only scalar variables. See http://dev.mysql.com/doc/refman/5.6/en/set-statement.html
You can INSERT from the results of a query with a join, but the query must be introduced with SELECT. See http://dev.mysql.com/doc/refman/5.6/en/insert-select.html
You can't use session variables as the names of tables. You would have to use a prepared statement. See http://dev.mysql.com/doc/refman/5.6/en/prepare.html But that opens a whole different can of worms, and doing it wrong can be a security vulnerability (see http://xkcd.com/327). I wouldn't recommend you start using prepared statements as a self-described MySQL-noob.
This problem is probably simpler than you're making it. You don't need a temporary table, and you don't need to read the results one row at a time.
Here's an example that I think does what you intend:
USE myDatabase
SET #ID = 1;
SET #maxID = 3;
SELECT sp.v, si.ttc
FROM speedTable AS sp
INNER JOIN sightsTable AS si
ON (sp.trip_id = si.trip_id AND 0.04 > ABS(sp.time_sec - si.time_sec))
WHERE sp.trip_id BETWEEN #ID AND #maxID;

Problems with syntax on MySQL trigger in PHP My Admin

I am having problems getting this trigger to work. Here is the code:
BEGIN
DECLARE newprice double;
DECLARE id int;
DECLARE rentdate DATE;
SET id := RESERVATION.RES_ID;
SET rentdate := "SELECT RES_RENT_DATE
FROM RESERVATION
WHERE RES_ID = id";
SET newprice := (NEW.RES_RETURN_DATE - rentdate)*RESERVATION.RES_CAR_PPD;
UPDATE RESERVATION
SET RESERVATION.RES_TOTAL_PRICE = newprice WHERE
RESERVATION.RES_ID = id;
END
Basically what I want to do is just update the total price when the return date of a car is changed. The trigger should execute on update of the Return Date. When updating it gives me the error: #1109 - Unknown table 'RESERVATION' in field list . I do not know what I am doing wrong.
There are several issues:
You can't arbitrarily refer to table columns like you did in SET id := RESERVATION.RES_ID;. You can refer to columns only either in a valid SQL statement (e.g. SELECT) or through NEW/OLD.
You can't issue DML statement (in your case UPADTE) on the same table on which you defined the trigger. This mutating behavior is prohibited in MySQL.
If I understand correctly and all values you need are in the same row, all you need is to use a BEFORE trigger. See example below.
Your trigger can be boiled down to this
CREATE TRIGGER tg_bu_reservation
BEFORE UPDATE ON reservation
FOR EACH ROW
SET NEW.res_total_price = (NEW.res_return_date - NEW.res_rent_date) * NEW.res_car_ppd;
Note: since it's a one statement trigger now there is no need in BEGIN...END block and changing a DELIMITER.
Here is SQLFiddle demo

Weird issue with a stored procedure in MySQL

I need to add a new stored procedure on our company's MySQL server. Since it's just slightly different, I used an already existing one, added the additional field and changed the name of the procedure. The weird thing now is that when I want to execute the statement, it returns:
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 '' at line 3
reffering to the 0 in this line: SET #update_id := 0; What makes it weird is, that I queried that stored procedure by using SHOW CREATE PROCEDURE . It's saved in our database and is working fine. I just can't use it as a new stored procedure (no matter if I try to apply it to the new test database or if I use it on the existing database by giving it a new name).
I searched the internet for a solution. Unfortunately to no avail. I even set up a new database with a new table and some demo values where I tried to execute the original, unaltered stored procedure. It returns the exact same error.
Here's the currently used and working stored procedure I'm talking about:
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
SET #update_id := 0;
UPDATE customer_shop SET state = 1, id = (SELECT #update_id := id), instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment WHERE tariff=Itariff AND state = 0 LIMIT 1;
SELECT * FROM customer_shop WHERE id = #update_id;
END
I hope you guys can help me as I am completely out of ideas what's wrong. :/
Regards, Mark
You need to define an alternative command delimiter, as MySQL currently thinks your CREATE PROCEDURE command ends at the first ; it encounters (on line 3, after the 0), which would be a syntax error as it's after a BEGIN but before the corresponding END:
DELIMITER ;; -- or anything else you like
CREATE PROCEDURE
...
END;; -- use the new delimiter you chose above here
DELIMITER ; -- reset to normal
MySQL stored procedures do not use ":=" for value assignment, just use "=".
Also don't think "id = (SELECT #update_id := id)" is acceptable. Here's an alternative solution (untested):
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
select id into #update_id from customer_shop WHERE tariff=Itariff AND state = 0 LIMIT 1;
UPDATE customer_shop SET state = 1, instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment where id = #update_id;
SELECT * FROM customer_shop WHERE id = #update_id;
END
You may also want to put error handlers in case there's no matching row to be edited.