Unknown column in 'where clause' failing in procedure - mysql

I'm trying to run the following code in a procedure. It is the very first line of code in the procedure after the BEGIN
update `deleteRequestsInitial`
SET `mamaID` = (SELECT `id` FROM `love_users`
WHERE `love_users`.`email` = `deleteRequestsInitial`.`Email`);
When I run it in the phpmyadmin SQL environment, it works fine. But when I call the procedure, I get the error:
Unknown column 'deleteRequestsInitial.Email' in 'where clause'
I've looked through a lot of the posts on the 'Unknown column' error, but none of the suggested workarounds seem to be working for me.

You are probably really running this:
update `deleteRequestsInitial`
SET `mamaID` = (SELECT `id` FROM `love_users`
WHERE `love_users`.`email` = `deleteRequestsInitial.Email`);
Or possibly, the column is email, not Email.
I would suggest just dropping all the back ticks:
UPDATE deleteRequestsInitial dri
SET mamaID = (SELECT id
FROM love_users lu
WHERE lu.email = dri.Email
);

Related

Bypass mysql error if column does not exist

I am working on a mysql procedure that I can run on all of my client environments however I am running into one issue. Some of my clients have a particular column on the database and others do not.
I am trying to use update ignore but I still get an error if the column doesnt exist.
I know I can query information schema for a count where that column exists, but I'm wondering if there is a more simplistic way to achieve this.
update ignore table1 set columnA = null;
ERROR 1054 (42S22): Unknown column 'columnA' in 'field list'
You have two options:
First solution: Check first if the column exists.
SELECT t.table_name, (c.column_name IS NULL) AS columnA_present
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT OUTER JOIN INFORMATION_SCHEMA.COLUMNS AS c
ON t.table_name = c.table_name and c.column_name = 'columnA'
WHERE t.table_name = 'table1';
Second solution: Run the UPDATE and catch the error. Check if the error is 1054. If so, then skip it. If not, then it's some other error so report it.

How to fix this "Invisible" syntax error in MySql query? (Using UPDATE inside TRIGGER)

I have the following MySQL query to create a trigger:
CREATE TRIGGER `updateParentLastInteractionOnInsert`
AFTER INSERT
ON `Post` FOR EACH ROW
BEGIN
UPDATE `Post` SET `lastInteraction` = CURRENT_TIMESTAMP WHERE `id` = new.`parentPostId`;
END;
When I execute the query, 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 '' at line 5
Since this means literally nothing, I've done my own research:
When I remove everything in the trigger's body (the statements between "BEGIN" and "END"), the query is processed successfully and an empty trigger is created.
So I've tested the inner statement by running the following query with a similar syntax:
UPDATE `Post` SET `lastInteraction` = CURRENT_TIMESTAMP WHERE `Post`.`id` = 1
which was also successfully executed.
I don't know what's wrong here. If both the trigger and the inner statements are syntactically & logically correct, how can the top query be syntactically incorrect? Maybe update statements aren't compatible with triggers? I've looked into MySql's documentation and around the web and I couldn't find any relevant information.
What's wrong in my query?
#BillKarwin 's comment has the answer.
This works:
DELIMITER //
CREATE TRIGGER `updateParentLastInteractionOnInsert`
AFTER INSERT
ON `Post` FOR EACH ROW
BEGIN
UPDATE `Post` SET `lastInteraction` = CURRENT_TIMESTAMP WHERE `id` = new.`parentPostId`;
END //

Check if a field exists and it's not null

I need to fetch a value from a table with three possibilities: 1) The field exists and it's not null, so it returns the value of the field 2) the field exists but it's null, so it returns is-null-string 3) The field doesn't exists, so it returns not-existing-string
I am trying to run this query but I get this error message #1054 - Unknown column 'm.my_field' in 'field list'
SELECT if (exists(SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'my_table' AND COLUMN_NAME = 'my_field'
), IFNULL(m.my_field, 'is-null-string'), 'not-existing-string'
) AS my_field,
m.*
FROM my_table m
Any idea how can I get it done in mysql 5.6?
What you are looking for is DESCRIBE:
DESCRIBE my_table;
Look at the result to see if your field exists. Note, you don't want this code to be in your application. You need to know your table fields to run queries. You might only use this to generate some boilerplate code.

How to properly create a trigger in workbench?

I am trying to create a simple triger functionality in my DB, so I can change a value in one of my tables when the other one is updated.
I have an oversimplified library database and the tables in question are boakloan and bookcopy.
In bookloan I have a foreign key to bookcopy, where I have an isAvailable column. The defaul value of this column is 1(true), and having a new entry in my bookloan table would mean that a copy of a book is borrowed, and it's no longer available, so I want to change the value of isAvailable to 0(false) for this record.
I tried to write a trigger for that in a couple of ways, but nothing worked.
Here is my last code :
USE `libdb`;
DELIMITER $$
DROP TRIGGER IF EXISTS libdb.bookcopy_update$$
USE `libdb`$$
CREATE DEFINER=`root`#`localhost` TRIGGER `libdb`.`bookcopy_update`
BEFORE INSERT
ON `bookloan` FOR EACH ROW
BEGIN
UPDATE bookcopy
SET bookcopy.isAvailable = 0
WHERE bookcopy.isAvailable = 1
AND bookcopy.idBookCopy = bookloan.BookCopy_idBookCopyFK;
END$$
DELIMITER ;
Can someone help make this work?
With the current version I get the following error : ERROR 1054: 1054: Unknown column 'bookloan.BookCopy_idBookCopyFK' in 'where clause'
You get that error because in this query (from your trigger body):
UPDATE bookcopy
SET bookcopy.isAvailable = 0
WHERE bookcopy.isAvailable = 1
AND bookcopy.idBookCopy = bookloan.BookCopy_idBookCopyFK;
there's no bookloan defined.
Replace bookloan.BookCopy_idBookCopyFK with NEW.BookCopy_idBookCopyFK.

correct code for concatenating substrings that are part of a particular field of a table to create new field

I am using MySQL 5.6.26 with Sequel Pro on Mac OS X. I would like to create a new field called Game_date in the table starting_pitcher_game_log. In order to create this field, I have to extract and attach substrings from another field in this table called GAME_ID. This is my code:
ALTER TABLE starting_pitcher_game_log ADD Game_date INTEGER;
UPDATE starting_pitcher_game_log
SET Game_date = CONCAT(SUBSTR(GAME_ID,4,4),'-',SUBSTR(GAME_ID,10,2),'-',SUBSTR(GAME_ID,12,2))
FROM game
WHERE game.GAME_ID = starting_pitcher_game_log.GAME_ID
I get the following error message:
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 game WHERE game.GAME_ID =
starting_pitcher_game_log.GAME_ID' at line 3
Can someone offer guidance on what's wrong with the code here?
Please let me know if you need additional information.
Thank you in advance.
update of status following Subin's code changes:
Thanks for your help Subin. Not working. I deleted field with null values. Then I put in your original code:
ALTER TABLE starting_pitcher_game_log ADD Game_date INTEGER;
UPDATE starting_pitcher_game_log
INNER JOIN game ON game.GAME_ID = starting_pitcher_game_log.GAME_ID
SET starting_pitcher_game_log.Game_date = CONCAT(SUBSTR(game.GAME_ID,4,4),'-',SUBSTR(game.GAME_ID,10,2),'-',SUBSTR(gam‌​e.GAME_ID,12,2))
It gives an error: Unknown column 'starting_pitcher_game_log.Game_date' in 'field list'. When I add "COLUMN" after "ADD", the query runs but only adds null values to the column.
I'm not sure what I'm doing wrong.
view of sequel pro of MySQL database table in question "starting_pitcher_game_log"
Subin, With your changes, the query runs eery time, but it still leaves the Game_id column with "null" on each row of that column. above is a screenshot of the table contents view of the starting_pitcher_game_log table. the "Game_ID" column is there, and this is where I have been trying to extract the information from to create the Game_date column--the date is expressed between character numbers 4 through 11. I'm not sure if this will help.
In any case, thank you for your help.
Update:
Here is the code that worked:
ALTER TABLE retrosheet.starting_pitcher_game_log ADD COLUMN Game_Date DATE;
UPDATE retrosheet.starting_pitcher_game_log AS b,
retrosheet.game AS g
SET b. Game_Date = CONCAT(SUBSTR(g.`GAME_ID`,4,4),'-',SUBSTR(g.`GAME_ID`,8,2),'-',SUBSTR(g.`GAME_ID`,10,2))
WHERE b.`Game_ID` = g.`GAME_ID`
You can use JOIN with your UPDATE().
ALTER TABLE starting_pitcher_game_log ADD Game_date VARCHAR(15);
UPDATE starting_pitcher_game_log
INNER JOIN game ON game.`GAME_ID` = starting_pitcher_game_log.`GAME_ID`
SET starting_pitcher_game_log.`Game_date` = CONCAT(SUBSTR(game.`GAME_ID`,4,4),'-',SUBSTR(game.`GAME_ID`,10,2),'-',SUBSTR(game.`GAME_ID`,12,2))
Alternative way of achieving same result without using JOIN keyword .
UPDATE starting_pitcher_game_log ,game
SET starting_pitcher_game_log.`Game_date` = CONCAT(SUBSTR(game.`GAME_ID`,4,4),'-',SUBSTR(game.`GAME_ID`,10,2),'-',SUBSTR(game.`GAME_ID`,12,2))
WHERE game.`GAME_ID` = starting_pitcher_game_log.`GAME_ID`
Hope this helps.